Results 1 to 4 of 4
- 02-15-2010, 12:11 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
Read and Write depending on file contents
Hi All,
I am fairly new to java, i am trying to write a simple file that reads the contents of a file and then dependent on the contents it writes a new value to a file.
It is a basic file that says either Logged In or Logged Out
I can get a application to read from a file and write to a file seperatley but when i try to combine them with a if then else statement i get a few error messages such as "try without catch" and illegal start of type catch (IOException e)...
Here is the code I am using, can anyone give me any pointers:
import java.io.*;
public class Write
{
// Main method
public static void main (String args[])
{
// Stream to write file
FileOutputStream fout;
// Stream to read file
FileInputStream fin;
try
{
fin = new FileInputStream ("test.txt");
fout = new FileOutputStream ("test.txt");
if (fin = "Logged In");
{
// Open an input stream
fout = new FileOutputStream ("test.txt");
// Print a line of text
new PrintStream(fout).println ("Logged Out");
// Close our output stream
fout.close();
}
else if
{
// Open an output stream
fout = new FileOutputStream ("test.txt");
// Print a line of text
new PrintStream(fout).println ("Logged In");
// Close our output stream
fout.close();
}
// Display the status to the user
System.out.println ("You have been " + fin);
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Login/Logout Failed.");
System.exit(-1);
}
}
}
- 02-15-2010, 12:27 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You should use Readers and Writers for character data not streams.
This line is definitely wrong
for several reasons.Java Code:if (fin = "Logged In");
Chief among them being
1.) trying to assign a string to a FileInputStream object.
2.) attempting to do an equals check on strings using = instead of using the equals method.
3.) the evil semicolon that is at the end of the if implying that the if is useless.
- 02-15-2010, 12:35 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
Thanks r035198x
I noticed the ";" just before I recieved your post.
What should I assign to the FileInputStream object if not a string?
I have tested the equals check using "==" instead of the "=" and i get an error:
"Wite.java:21: incomparable types: java.io.FileInputStream and java.lang.String if (fin == "Logged In"){
What should I be using for the equals if not == or !=, Thanks.
- 02-15-2010, 12:38 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read from and write to .properties file from a jsp
By MAGNUM in forum New To JavaReplies: 5Last Post: 01-20-2009, 09:08 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 12:11 PM -
File read/write problems
By p900128 in forum New To JavaReplies: 4Last Post: 06-27-2008, 12:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks