problem in writing data in file......

i am having problem in passing a String object path ;

taken from user in following way

FileWriter fw = new FileWriter(path,append);

error is : (The filename, directory name, or volume label syntax is incorrect)

but if i am using

FileWriter fw = new FileWriter("f.txt",append);

it works...

but i want filename from user... how to achieve this

[392 byte] By [BRAVOa] at [2007-11-27 7:50:02]
# 1
So you pass wrong value in path variable.
Michael.Nazarov@sun.coma at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks but....i have tried this way alsoString path = "E:/fname.txt";FileWriter fw = new FileWriter(path,append);wont work....but if this way ...FileWriter fw = new FileWriter("E:/fname.txt",append);it works....whats happening...
BRAVOa at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 3
Are you sure you use "e:/file" not "e:\file"?
Michael.Nazarov@sun.coma at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 4
yes sure......
BRAVOa at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 5
Really really?Because "e:/file.txt" works fine while "e:\file.txt" throws exception you post: The file name, directory name, or volume label syntax is incorrect):)
Michael.Nazarov@sun.coma at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 6
could u please give simple example where a user inputs the name of the filethen he enters the data and the data is entered in a file having name given by the user...not predefined in the program
BRAVOa at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 7
You already have such code as I understand your question.
Michael.Nazarov@sun.coma at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 8
i need one which works mine is not working .....pls help
BRAVOa at 2007-7-12 19:31:01 > top of Java-index,Java Essentials,Java Programming...
# 9
Show you code we'll find problem :)
Michael.Nazarov@sun.coma at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 10

class writeData

{

public String getName()

{

String name=null;

try

{

char c;

StringBuffer bf=new StringBuffer();

System.out.print("Enter the File Name:- ");

while((c=(char)System.in.read())!='\n')

{

bf.append(c);

}

name=bf.toString();

}

catch(Exception e)

{

System.out.println("Error is:-"+e);

}

return name;

}

public void wfile(String name1)

{

try

{

String path ="D:/fname.txt";

boolean append = true;

FileWriter fw=new FileWriter(path); // its not working....

//FileWriter fw=new FileWriter("D:/fname.txt");

}

catch(Exception e)

{

System.out.println("Error is :- "+e);

}

}

}

public class bravo

{

public static void main(String args[])

{

writeData wd = new writeData();

String n = wd.getName();

wd.wfile(n);

}

}

BRAVOa at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 11
Hmmi have in some of my properties Files some paths which look like this:C:\\Program Files\\Some Program\\Some File.txtwith double (!) backslashes.I guess the problem is related to the slash.What is the outcome if you use \\
BugBunnya at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 12

Please use code tags.

Anyway your code works fine for me in this version.

But yes, you have problem with name1 (not with path): while reading file name you also read terminating '\r', so entering abc.txt you are trying to create abc.txt\r actually - this name is wrong. You can correct your code changing '\n' to '\r' - this is fast but wrong way. It's better to use another method of reading.

Michael.Nazarov@sun.coma at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 13

thanks.....

FileInputStream fstream = new FileInputStream("Emp.txt");

DataInputStream dstream = new DataInputStream(fstream);

BufferedReader bf = new BufferedReader(new InputStreamReader(dstream));

String data = null;

String comma = ",";

while((data = bf.readLine()) != null)

BRAVOa at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 14
The DataInputStream in this stack is unnecessary.
ejpa at 2007-7-12 19:31:02 > top of Java-index,Java Essentials,Java Programming...
# 15

> The DataInputStream in this stack is unnecessary.

it's actually erroneous (or at least counter productive) isn't it?

I mean doesn't DataInputStream do some funky charset conversion instead of dealing with plain UTF char's?

Probably doesn't matter as long as the @OP is only reading base ascii, but then I'm getting fussier about correctness as I get older ;-)

Keith.

corlettka at 2007-7-21 22:22:48 > top of Java-index,Java Essentials,Java Programming...
# 16
> it's actually erroneous (or at least counter> productive) isn't it?No, just pointless.> I mean doesn't DataInputStream do some funky charset> conversion instead of dealing with plain UTF char's?No. You're thinking about Readers.
ejpa at 2007-7-21 22:22:48 > top of Java-index,Java Essentials,Java Programming...