The Artima Developer Community
Sponsored Link

Java Answers Forum
read file puzzled

1 reply on 1 page. Most recent reply: Apr 17, 2003 3:06 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
eastjy

Posts: 1
Nickname: eastjy
Registered: Apr, 2003

read file puzzled Posted: Apr 16, 2003 7:42 PM
Reply to this message Reply
Advertisement
import java.io.*;
class ReadFile
{
public static void main(String args[]) throws Exception
{
//String ss="d:\\Temp\\Test.java";
//File readFile = new File(ss);

StringBuffer stfDir = new StringBuffer();
char ch = (char)System.in.read();
while( ch != '\n')
{
stfDir.append(ch);
ch = (char)System.in.read();
}

String ss = stfDir.toString();
File readFile = new File( ss );

if( readFile.isFile() &&readFile.canWrite() && eadFile.canRead())
{
RandomAccessFile rafFile =
new RandomAccessFile(readFile,"rw");
while(rafFile.getFilePointer()<rafFile.length())
System.out.println(rafFile.readLine());
rafFile.close();
}
else
System.out.println("File cannot be read!");
}
}
</pre>
run input d:\temp\test.java(the file exists),the result is: File cannot be read!
but this file compile and run in JBuilder,the result is OK.
and I use the sentence which is commented,the result is right not only in JBuilder but JDK self.

another is the sentense list underside in java is not be allowed??
while(char ch = (char)System.in.read())!='\n')


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: read file puzzled Posted: Apr 17, 2003 3:06 AM
Reply to this message Reply
I suspect it might be something to do with the fact that '\' is the escape character.

Adam

Flat View: This topic has 1 reply on 1 page
Topic: Difficulties on Java2 RMI application Previous Topic   Next Topic Topic: Writing files in Java additional question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use