AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

newbie... error finding file

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
jim barr
Guest





PostPosted: Wed Sep 17, 2003 1:42 pm    Post subject: newbie... error finding file Reply with 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
Nathan Zumwalt
Guest





PostPosted: Thu Sep 18, 2003 8:19 pm    Post subject: Re: newbie... error finding file Reply with quote



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





PostPosted: Sun Sep 21, 2003 5:27 pm    Post subject: Re: newbie... error finding file Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.