Results 1 to 4 of 4
  1. #1
    paulhenderson is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Unhappy 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);
    }
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should use Readers and Writers for character data not streams.

    This line is definitely wrong
    Java Code:
    if (fin = "Logged In");
    for several reasons.
    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.

  3. #3
    paulhenderson is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) You are still usings streams. Use FileReader and FileWriter instead.
    2.) You are still comparing a FileInputstream with a String.

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  2. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  3. Replies: 5
    Last Post: 01-20-2009, 09:08 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM
  5. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •