Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-19, 07:38pm
java file output
Subject: java file output  ·  Posted: 2003-10-09, 04:35am
Rank: Unregistered
i am attempting to write information to a file with this function

Code:
  1. public void save()
  2.     {
  3.         try
  4.         {
  5.             FileOutputStream fileOutputStream = new FileOutputStream("datafile.dat");
  6.             PrintWriter printWriter = new PrintWriter(fileOutputStream, true);
  7.             
  8.             String [] th = new String[5];
  9.             
  10.             for(int i = 0; i < studentCount; i++)
  11.             {
  12.                 th = students[i].fOutput();
  13.                 
  14.                 for(int j = 0; j < 5; j++)
  15.                     printWriter.print(th[j]);
  16.             }
  17.             fileOutputStream.close();
  18.         }
  19.         catch(IOException e)
  20.         {
  21.             messageBox("Error opening file " + e.toString());
  22.         }
  23.     }


this will create the file if it is not created, but it will not save information when the function is run

however, if i insert information into the file manually, it will be brought into the program by my read function, which is nearly identical

Code:
  1. public void read()
  2.     {
  3.         try
  4.         {
  5.             FileInputStream stream = new FileInputStream("datafile.dat");
  6.             InputStreamReader iStrReader = new InputStreamReader(stream);
  7.             BufferedReader reader = new BufferedReader(iStrReader);
  8.             String [] line = new String [4];
  9.             String gfl = new String();
  10.             gfl = reader.readLine();
  11.         
  12.             while(gfl != null)
  13.             {
  14.                 for(int i = 0; i < 4; i++)
  15.                     line[i] = reader.readLine();
  16.                         
  17.                 Student stu = new Student(line[0], line[1], line[2], line[3]);
  18.                 students[studentCount] = stu;
  19.                     
  20.                 gfl = reader.readLine();
  21.                     
  22.                 studentCount++;
  23.             }
  24.             stream.close();
  25.         }
  26.         catch(IOException e)
  27.         {
  28.             messageBox("Error opening file " + e.toString());
  29.         }
  30.     }


any help is appreciated
 
  Reply to this ·  Post link ·  Top
Subject: Re: java file output  ·  Posted: 2003-10-10, 03:25am
Rank: ? (4827)
Member #: 3416
i added code tags to make that post easier to read.

line 10 in the write to a file code tries to loop based on studentCount, which has not been defined. it shouldn't compile if you didn't have it declared somewhere, but i don't see it declared in the function or getting passed in. also it doesn't look like it's a constant. i would expect that to be your problem though, that it's seeing that value as a 0 so it never executes the body of the loop.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs