 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jim barr Guest
|
Posted: Wed Sep 17, 2003 1:42 pm Post subject: newbie... error finding file |
|
|
Hi all,
Just found this group, it may be appropriate for me as a newbie!
The following code cam straight from the book, but cannot find the file
"text.txt", My understanding is that it should be creating the file!!
Any ideas, Thanks, Jim
import java.io.*;
public class keytodisk
{
public static void main(String args[])
throws IOException
{
String str;
FileWriter fw;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
fw = new FileWriter("C:test.txt");
}
catch(IOException exc)
{
System.out.println("Cannot open file");
return;
}
System.out.println("Enter text ('stop' to quit).");
do
{
System.out.print(":");
str = br.readLine();
if (str.compareTo("stop") == 0) break;
str = str + "rn" ; //add new line
fw.write(str);
}
while(str.compareTo("stop") != 0);
fw.close();
}
}
|
|
| Back to top |
|
 |
Nathan Zumwalt Guest
|
Posted: Thu Sep 18, 2003 8:19 pm Post subject: Re: newbie... error finding file |
|
|
Look at the API for FileWriter:
http://java.sun.com/j2se/1.4.1/docs/api/java/io/FileWriter.html
The second paragraph states that this type of auto-creation of files
is platform specific. To be safe, use the File object to test for the
files existence, and create a new file if needs be.
-Nathan
"jim barr" <wandana (AT) ntlworld (DOT) com> wrote
| Quote: | Hi all,
Just found this group, it may be appropriate for me as a newbie!
The following code cam straight from the book, but cannot find the file
"text.txt", My understanding is that it should be creating the file!!
Any ideas, Thanks, Jim
import java.io.*;
public class keytodisk
{
public static void main(String args[])
throws IOException
{
String str;
FileWriter fw;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
fw = new FileWriter("C:test.txt");
}
catch(IOException exc)
{
System.out.println("Cannot open file");
return;
}
System.out.println("Enter text ('stop' to quit).");
do
{
System.out.print(":");
str = br.readLine();
if (str.compareTo("stop") == 0) break;
str = str + "rn" ; //add new line
fw.write(str);
}
while(str.compareTo("stop") != 0);
fw.close();
}
}
|
|
|
| Back to top |
|
 |
Luc Gyselinck Guest
|
Posted: Sun Sep 21, 2003 5:27 pm Post subject: Re: newbie... error finding file |
|
|
You used a '' in the file name string : "C:test.txt"
A '' is used for escape characters...
Either use a '/' or a double '\'!
"jim barr" <wandana (AT) ntlworld (DOT) com> wrote
| Quote: | Hi all,
Just found this group, it may be appropriate for me as a newbie!
The following code cam straight from the book, but cannot find the file
"text.txt", My understanding is that it should be creating the file!!
Any ideas, Thanks, Jim
import java.io.*;
public class keytodisk
{
public static void main(String args[])
throws IOException
{
String str;
FileWriter fw;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try
{
fw = new FileWriter("C:test.txt");
}
catch(IOException exc)
{
System.out.println("Cannot open file");
return;
}
System.out.println("Enter text ('stop' to quit).");
do
{
System.out.print(":");
str = br.readLine();
if (str.compareTo("stop") == 0) break;
str = str + "rn" ; //add new line
fw.write(str);
}
while(str.compareTo("stop") != 0);
fw.close();
}
}
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|