Reputation: 57
I've a textfile called backUp.dat. That file contains a directory path, so I created a simple method to read that file:
public static String readFileAsString(String filename) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(filename));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
//System.out.println("Current Directory:"+ new File(".").getAbsolutePath());
return sb.toString();
} catch (FileNotFoundException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Il Manque un Fichier de System Contactez Votre Fournisseur","ERREUR",JOptionPane.ERROR_MESSAGE);
System.exit(0);
} catch (IOException ex) {
Logger.getLogger(Welcome.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
This work perfectly, but here is the problem:
String dir = readFileAsString("backUp.dat");
File fileEES = new File(dir+"backUp.XML");
if (fileEES.createNewFile()) {
System.out.println("File is created!");
} else {
System.out.println("File already exists.");
}
I get this exception:
sept. 08, 2015 10:22:28 PM credittaxiphone.FirstWindow exporterUserInfo
GRAVE: null
java.io.IOException: La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at credittaxiphone.FirstWindow.exporterUserInfo(FirstWindow.java:565)
at credittaxiphone.FirstWindow.exit(FirstWindow.java:116)
at credittaxiphone.FirstWindow.formWindowClosing(FirstWindow.java:413)
at credittaxiphone.FirstWindow.access$100(FirstWindow.java:42)
at credittaxiphone.FirstWindow$1.windowClosing(FirstWindow.java:179)
at java.awt.Window.processWindowEvent(Window.java:2058)
at javax.swing.JFrame.processWindowEvent(JFrame.java:305)
at java.awt.Window.processEvent(Window.java:2017)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
On the line:
File fileEES = new File(dir + "backUp.XML");
If I remove dir
it works correctly.
The content of backUp.dat is:
D:\\
Upvotes: 2
Views: 98
Reputation: 5137
If you look at the JavaDocs, you'll see that the File constructor you are trying to use takes a parent directory as the first argument, and a file name as the second argument.
Here is the code you currently have:
String dir=readFileAsString("backUp.dat");
File fileEES = new File(dir+"backUp.XML");
But the API requires something like this:
File dir = new File(readFileAsString("backUp.dat"));
File fileEES = new File(dir, "backUp.XML");
Please note that these examples do not account for proper exception handling. For a complete reference, see the JavaDoc.
public File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.
Otherwise the parent abstract pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty abstract pathname then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.
Parameters: parent - The parent abstract pathname child - The child pathname string Throws: NullPointerException - If child is null
Upvotes: 0
Reputation: 8793
Most probably the cause of the filename error is the newline you are appending to each read line in readFileAsString
. If you expect backUp.dat
to contain just a directory path, you should not append any newlines.
And take care when adding dir
and "backUp.XML"
: A directory separator char (File.separatorChar
) might be needed there.
Upvotes: 2