For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2006 > Frustrated trying to Read File









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Frustrated trying to Read File
bH

2006-10-18, 7:07 pm

Hi All,
I have this program below but it won't compile without errors.
The errors reported at this time are noted below the program.

Help is appreciated.

bH


import java.io.*;

public class ReadFile {
String strArry[] = new String[15];
public static void main (String args[])throws Exception{
//System.out.println("here");
ReadFile rFile = new ReadFile();
rFile.test();
} //main

public void test(){
try{
FileInputStream fis = new
FileInputStream("NameAgeWeightFile.txt");
BufferedReader bor = new BufferedReader(new
InputStreamReader(fis));

for(int i=0; i<15; i++){
strArry[i] = bor.readLine();
} //end of for
bor.close();
} //end of try
catch (Exception e) {
System.out.println("error getting data");
} //end of catch
for(int ic=0; ic<15; ic++){
System.out.println (strArry[ic]);
} //end of for
} //end of test
} //end of ReadFile


error list:
ReadFile.java, Line 16 : Class BufferedReader not found in type
declaration.
BufferedReader bor = new BufferedReader (new
InputStreamReader(fis));
^
ReadFile.java, Line 16 : Class BufferedReader not found in type
declaration.
BufferedReader bor = new BufferedReader (new
InputStreamReader(fis));

^
ReadFile.java, Line 19 : Class BufferedReader not found in void test().
strArry[i] = bor.readLine();
^
ReadFile.java, Line 19 : Class BufferedReader not found in void test().
strArry[i] = bor.close();
^

Ian Shef

2006-10-18, 7:07 pm

"bH" <bherbst65@hotmail.com> wrote in news:1161198597.015168.180950
@b28g2000cwb.googlegroups.com:

> Hi All,
> I have this program below but it won't compile without errors.
> The errors reported at this time are noted below the program.
>
> Help is appreciated.
>
> bH
>
>
> import java.io.*;
>
> public class ReadFile {
> String strArry[] = new String[15];
> public static void main (String args[])throws Exception{
> //System.out.println("here");
> ReadFile rFile = new ReadFile();
> rFile.test();
> } //main
>
> public void test(){
> try{
> FileInputStream fis = new
> FileInputStream("NameAgeWeightFile.txt");
> BufferedReader bor = new BufferedReader(new
> InputStreamReader(fis));
>
> for(int i=0; i<15; i++){
> strArry[i] = bor.readLine();
> } //end of for
> bor.close();
> } //end of try
> catch (Exception e) {
> System.out.println("error getting data");
> } //end of catch
> for(int ic=0; ic<15; ic++){
> System.out.println (strArry[ic]);
> } //end of for
> } //end of test
> } //end of ReadFile
>
>
> error list:
> ReadFile.java, Line 16 : Class BufferedReader not found in type
> declaration.
> BufferedReader bor = new BufferedReader (new
> InputStreamReader(fis));
> ^
> ReadFile.java, Line 16 : Class BufferedReader not found in type
> declaration.
> BufferedReader bor = new BufferedReader (new
> InputStreamReader(fis));
>
> ^
> ReadFile.java, Line 19 : Class BufferedReader not found in void test().
> strArry[i] = bor.readLine();
> ^
> ReadFile.java, Line 19 : Class BufferedReader not found in void test().
> strArry[i] = bor.close();
> ^
>
>


Compiled fine for me under JDK 1.5.0_08, using Eclipse 3.2.0.
Check your setup (environment, compiler, IDE, etc.).

When run, produced:
error getting data
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

Wll, what did you expect? I didn't have a file NameAgeWeightFile.txt

Good luck!


--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
blmblm@myrealbox.com

2006-10-21, 10:03 pm

In article <1161198597.015168.180950@b28g2000cwb.googlegroups.com>,
bH <bherbst65@hotmail.com> wrote:
> Hi All,
> I have this program below but it won't compile without errors.
> The errors reported at this time are noted below the program.
>
> Help is appreciated.
>


[ snip ]

> FileInputStream fis = new
> FileInputStream("NameAgeWeightFile.txt");
> BufferedReader bor = new BufferedReader(new
> InputStreamReader(fis));


Is there a reason you didn't just write the following?

BufferedReader bor =
new BufferedReader(new FileReader("NameAgeWeightFile.txt");

"Only trying to help"?

[ snip ]

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
bH

2006-10-22, 7:07 pm


blmblm@myrealbox.com wrote:
> In article <1161198597.015168.180950@b28g2000cwb.googlegroups.com>,
> bH <bherbst65@hotmail.com> wrote:
>
> [ snip ]
>
>
> Is there a reason you didn't just write the following?
>
> BufferedReader bor =
> new BufferedReader(new FileReader("NameAgeWeightFile.txt");
>
> "Only trying to help"?
>
> [ snip ]
>
> --
> B. L. Massingill
> ObDisclaimer: I don't speak for my employers; they return the favor.




Thanks to both of you for your replies.

Seems that the problem is I am using a prehistoric iBook (1999 version)
with OS 9.2 and Java 1.0.3. It's a mini dinosaur. The first issue
seems to be a problem in that it has no "import" that connects with
the entries FileInputStream and BufferedReader.

The second issue that the OS and Java will not "read" files even if I
use a simplified version that complies with no error which is shown
below:

import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;

public class DemoRandomAccessFile {

private static void doAccess() {

try {
File file = new File("NameAgeWeightFile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");

// Read a character
byte ch = raf.readByte();
System.out.println("Read first character of file: " +
(char)ch);

// Now read the remaining portion of the line.
// This will print out from where the file pointer is
located
// (just after the '+' character) and print all remaining
characters
// up until the end of line character.
System.out.println("Read full line: " + raf.readLine());

// S to the end of file
raf.s(file.length());

// Append to the end of the file
raf.write(0x0A);
raf.writeBytes("This will complete the Demo");
raf.close();

} catch (IOException e) {
System.out.println("IOException:");
e.printStackTrace();
}
}

public static void main(String[] args) {
doAccess();
}

}

I need to figure out how to write a text file, one that this Java will
recognize: I have tried to do various forms of text files that are
saved using the name of "NameAgeWeightFile.txt (Simple text, Claris
Works, Appleworks etc ) then placing it in the same folder as the java
program, ..... , then use the above program to read it, But will not
read it :(.

Thanks again for your replies.

bH

blmblm@myrealbox.com

2006-10-22, 7:07 pm

In article <1161533791.438405.108330@e3g2000cwe.googlegroups.com>,
bH <bherbst65@hotmail.com> wrote:
>


[ snip ]

> Thanks to both of you for your replies.
>
> Seems that the problem is I am using a prehistoric iBook (1999 version)
> with OS 9.2 and Java 1.0.3. It's a mini dinosaur. The first issue
> seems to be a problem in that it has no "import" that connects with
> the entries FileInputStream and BufferedReader.
>
> The second issue that the OS and Java will not "read" files even if I
> use a simplified version that complies with no error which is shown
> below:
>
> import java.io.File;
> import java.io.RandomAccessFile;
> import java.io.IOException;
>
> public class DemoRandomAccessFile {
>
> private static void doAccess() {
>
> try {
> File file = new File("NameAgeWeightFile.txt");
> RandomAccessFile raf = new RandomAccessFile(file, "rw");
>
> // Read a character
> byte ch = raf.readByte();
> System.out.println("Read first character of file: " +
> (char)ch);
>
> // Now read the remaining portion of the line.
> // This will print out from where the file pointer is
> located
> // (just after the '+' character) and print all remaining
> characters
> // up until the end of line character.
> System.out.println("Read full line: " + raf.readLine());
>
> // S to the end of file
> raf.s(file.length());
>
> // Append to the end of the file
> raf.write(0x0A);
> raf.writeBytes("This will complete the Demo");
> raf.close();
>
> } catch (IOException e) {
> System.out.println("IOException:");
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args) {
> doAccess();
> }
>
> }


Well, the above code compiles and executes okay on a Linux system
running a 1.5 version of Java. That doesn't help you, maybe,
but at least indicates that the code isn't hopelessly broken?
Now, after running it, if I open NameAgeWeightFile.txt in a text
editor I notice that the line added by the writeBytes doesn't end
in a line-end character, which seems a little wrong, but it *is*
what the code said to do. Something else that seems a little wrong
is the write of a 0x0A character, which is the line-end character
on Unix systems but not on Windows or Mac, each of which have their
own conventions ....

> I need to figure out how to write a text file, one that this Java will
> recognize: I have tried to do various forms of text files that are
> saved using the name of "NameAgeWeightFile.txt (Simple text, Claris
> Works, Appleworks etc ) then placing it in the same folder as the java
> program, ..... , then use the above program to read it, But will not
> read it :(.


It might help if you said you mean by "will not read it" -- do
you get an exception? output other than what you think you should
get? something else??

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
bH

2006-10-22, 10:02 pm


blmblm@myrealbox.com wrote:
> In article <1161533791.438405.108330@e3g2000cwe.googlegroups.com>,
> bH <bherbst65@hotmail.com> wrote:
>
> [ snip ]
>
>
> Well, the above code compiles and executes okay on a Linux system
> running a 1.5 version of Java. That doesn't help you, maybe,
> but at least indicates that the code isn't hopelessly broken?
> Now, after running it, if I open NameAgeWeightFile.txt in a text
> editor I notice that the line added by the writeBytes doesn't end
> in a line-end character, which seems a little wrong, but it *is*
> what the code said to do. Something else that seems a little wrong
> is the write of a 0x0A character, which is the line-end character
> on Unix systems but not on Windows or Mac, each of which have their
> own conventions ....
>
>
> It might help if you said you mean by "will not read it" -- do
> you get an exception? output other than what you think you should
> get? something else??
>
> --
> B. L. Massingill
> ObDisclaimer: I don't speak for my employers; they return the favor.






Hi B. L. Massingill,

I have had similar outputs as you note above when I use my up to date
Windows

machine.

However when I use the iBook these errs are noted:

IOException:
java.io.EOFException
at java.io.RandomAccessFile.readByte(RandomAccessFile.java:250)
at DemoRandomAccessFile.doAccess(RandomAccessFile.java:14)
at DemoRandomAccessFile.main(DemoRandomAccessFile:38)
at sun.macos.StandaLoneRunner.main(StandaLoneRunner.java:26)

I have also observed that the Java 1.0.3 is creating a ghost Java icon
text file, different from the original file NameAgeWeightFile.txt and
it contains nothing, zero K, does not show up in the catalog listing
but is in the same folder as NameAgeWeightFile.txt and has the same
name. How do I know? I can do a search of the folder and it is
identified, once it is in the search listing, I can drag it to the
trash and empty it.

Let's face it..... this is a sick. Somebody must have made this java
version on a Friday afternoon and wanted get out to the beach before
the rest of the department.

And you and I have wasted a lot of time on something that can't be
twed into existance.

Thanks so much.

bH

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com