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
Please
login or
register to post a reply.
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:
public void save()
{
try
{
FileOutputStream fileOutputStream = new FileOutputStream("datafile.dat");
PrintWriter printWriter = new PrintWriter(fileOutputStream, true);
String [] th = new String[5];
for(int i = 0; i < studentCount; i++)
{
th = students[i].fOutput();
for(int j = 0; j < 5; j++)
printWriter.print(th[j]);
}
fileOutputStream.close();
}
catch(IOException e)
{
messageBox("Error opening file " + e.toString());
}
}
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:
public void read()
{
try
{
FileInputStream stream = new FileInputStream("datafile.dat");
InputStreamReader iStrReader = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(iStrReader);
String [] line = new String [4];
String gfl = new String();
gfl = reader.readLine();
while(gfl != null)
{
for(int i = 0; i < 4; i++)
line[i] = reader.readLine();
Student stu = new Student(line[0], line[1], line[2], line[3]);
students[studentCount] = stu;
gfl = reader.readLine();
studentCount++;
}
stream.close();
}
catch(IOException e)
{
messageBox("Error opening file " + e.toString());
}
}
any help is appreciated
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
Forum Quick Jump - Free2Code - Anything and Everything - Suggestions (Archived) - Challenges & Awards (Archived) - Chat (Archived) - Community Projects (Archived) - Bugs (Archived) - Programming Languages - PHP (Archived) - C/C++ (Archived) - Visual Basic (Archived) - Other (Archived) - Java (Archived) - Assembler (Archived) - Perl (Archived) - Python (Archived) - Programming - 3D Graphics (Archived) - Database design and management (Archived) - Physics (Archived) - Encryption and compression (Archived) - Networking (Archived) - Operating Systems (Archived) - Web Development - Graphics (Archived) - Web Design (Archived) - HTML/CSS (Archived) - Website Help/Review (Archived) - Flash (Archived) - Computing & Operating Systems - Hardware/Electronics (Archived) - Security (Archived) - Linux/UNIX (Archived) - Microsoft Windows (Archived) - Apple Mac (Archived) - Other OSs (Archived) - General Computing - Life/Other - Religion (Archived) - Politics/World Events (Archived) - Debate (Archived) - Arts & Entertainment - Books++ (Archived) - Music (Archived) - Movies (Archived) - Humor (Archived) - Games (Archived) - Sports (Archived) - Arts (Archived) - Archives - Tutorial Request (Archived) - Old Bugs 2 (Archived) - Free2Host Hosting (Archived) - Ada (Archived) - Functional Languages (Archived) - Career (Archived) - Old Bugs (Archived)
Pages: 1
Please
login or
register to post a reply.