Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Some trouble with File I/O [Java]

Tags:
  1. Mar 21, 2017 #1
    I'm trying to write Strings to files in Java.

    Basically, I'm supposed to read from a file containing text (assumed to be strings). For each line of text,
    if the String has the format word1 word2 (that is, if it's two words), I have to write it to one file (a list of names), whereas if it doesn't have that format, I have to write it to another file, which is supposed to be called System.out (with the extension .txt)

    I made a file and wrote some stuff on it to test my code with. Here's what it contains:

    Prima Secunda
    First Second
    Un Deux
    Uno Dos
    Ein Zwei
    This line is not valid
    Neither \.! is this |||; one
    Uno Due
    This should not be written

    I've got the list of names part working just fine. The names get written to the output file as they should:

    P.Secunda
    F.Second
    U.Deux
    U.Dos
    E.Zwei
    U.Due

    but when writing to the System.out file, only this line gets written:

    Neither \.! is this |||; one

    So the lines formatted like regular sentences ("This should not be written", etc) get skipped. I'm not sure why this is. Here's some excerpts from my code:


    Code (Java):
    public static void processData(BufferedReader inputFile, PrintWriter outputFile)
        {
         
            // A modified version of
            // the algorithm shown
            // on the class page.
         
            String inputLine = null;
         
            try
            {
                // Do/while loop that processes
                // each line taken from the file.
                // First it checks whether the
                // line has the format word1 word2

                while ((inputLine = inputFile.readLine()) != null)
                {              
                 
                    // Splits the line into tokens,
                    // taking the delimiter to be
                    // anything that is not an
                    // upper or lowercase letter
                    // or the tab character (\t)
                    // in groups of at least one
                    // (using the regex [^a-zA-Z]+)
                 
                    String[] tokens = inputLine.split("[^a-zA-Z\t]+");
                 
                    // REMOVE LINES 215-216
                    System.out.println("inputLine is: " + inputLine);
                    System.out.println("The length of tokens is: " + tokens.length);
                 
                    // If the line has the format
                    // word1 word2, the outputWriter
                    // method is invoked to process
                    // the words and write them to
                    // the output file
                 
                    if (tokens.length == 2){
                        outputWriter(tokens, outputFile);
                    }
                 
                    // Otherwise, the systemWriter
                    // method is invoked to write
                    // the line to the System.out file
                 
                    // REMOVE LINE 234
                    else{
                        System.out.println("ENTERED SYSTEMWRITER TREE AT: " + inputLine);
                        systemWriter(tokens);
                    }
                }
            }

    // Catch block that prints
            // the name of the exception
            // and the line it occured on
            // to the console
            catch (IOException ex)
            {
                System.out.println(ex + " on line: "
                        + inputLine);
            }
        }

    // Auxiliary method that writes
        // lines that do not conform
        // to the format specified
        // by the instructions to the
        // System.out file

    public static void systemWriter(String[] tokens)
        {
            // REMOVE LINES 297-303
         
            System.out.println("Tokens contains: ");
         
            for (String currentToken : tokens)
            {
                System.out.println(currentToken + " ");
            }
         
            File systemOutput = new File("System.out.txt");
         
            // Here we create a PrintWriter
            // to write to the System.out
            // file. Note that this does NOT
            // create the file itself, only
            // an object that manipulates it
            // Therefore there is no problem
            // in repeatedly creating the
            // systemFile instance each time this
            // method is invoked
         
            try
            {
                PrintWriter systemFile = new PrintWriter(new FileWriter(systemOutput));
             
                // As requested by instructions,
                // this message lets the user
                // know that the line in question
                // does not have the format word1
                // word2
             
                systemFile.println("The following line does not have the"
                        + " format word1 word2:");
                for (String currentWord : tokens)
                    systemFile.print(currentWord + " ");
             
                // Close the System.out file after modifying it
                systemFile.close();
            }
         
            catch (IOException ex)
            {
                System.out.println("Failed to create System.out file!");
            }  
        }  
     
    Does this maybe have something to do with my closing the output stream after using it in the try-catch block?
     
  2. jcsd
  3. Mar 21, 2017 #2

    Borg

    User Avatar
    Science Advisor
    Gold Member

    For a simple program like this, it would be better to collect all of the data that you're going to write and then write to each of the files. Passing readers and writers into a method is a good way to have them get into a bad state.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Some trouble with File I/O [Java]
  1. C++ Simple File I/O (Replies: 12)

  2. C file I/O (Replies: 3)

  3. FILE I/O in an IDE (Replies: 1)

  4. C++ file i/o (Replies: 7)

Loading...