The filename, directory name, or volume label syntax is incorrect
Hi,
im trying to create a (text)file with a user-specified content. The user input (filename and content) is saved in a string variable. But I'm getting the following error after the input of the variables:
java.io.IOException: The filename, directory name, or volume label syntax is incorrect.
The relevant method is:
publicvoid writeFile(String filename, String content){
File fi =new File(filename);
try{
if ( fi.exists() ){
fi.delete();
}
fi.createNewFile();
FileWriter fw =new FileWriter(fi);
fw.write(content);
fw.close();
}
catch(Exception e){
e.printStackTrace();
}
When I specify the filename and content manually (e.g.
fw.write("some content");
and
File fi =new File("Example.txt");
the application perfectly works without any errors. Can anybody help me with this?
Thanks.
[1432 byte] By [
eoxa] at [2007-10-2 13:20:13]

What are the parameters that you are sending to the method? If it works when you hard code the variables, then most likely the parameters that you are sending are somehow causing the problem.
Put two System.out lines as the first line of the method and see if the variables are what you think they should be.System.out.println("filename is " + filename + "length: " + filename.length());
System.out.println("content is " + content + "length: " + content.length());
The length will tell you if there are whitespace characters in the String.
Finally discovered the error. It was a problem in the Input-Method. Seemed to be an \n at the end of the Input.
This code now works:
import java.io.*;
public class Stream{
public Stream(){
}
//Input-Methode
public String stringInput(){
StringBuffer temp = new StringBuffer();
InputStreamReader ir = new InputStreamReader(System.in);
try {
do {
int i = ir.read();
if(i != 10 && i != 13)
temp.append((char) i);
} while ( ir.ready());
}
catch (IOException e) {
e.printStackTrace();
}
return temp.toString();
}
public void dateiSchreiben(String filename, String content){
File fi = new File(filename);
try{
//Delete existing file
if ( fi.exists() ) {
fi.delete();
}
fi.createNewFile();
//Write content to file
FileWriter fw = new FileWriter(fi);
fw.write(content);
fw.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main (String args[]){
String filename; //Filename
String content; //Content of the File
Stream se = new Stream();
System.out.println("Enter Filename: ");
filename = StdIn.stringInput();
System.out.println("Enter Content: ");
content = StdIn.stringInput();
se.dateiSchreiben(filename, content);
}
}
eoxa at 2007-7-13 10:55:04 >
