5 Replies Latest reply: Dec 9, 2007 8:42 PM by 800351 RSS

    Writing an contents from an ArrayList to a text file

    807603
      Hi, I am fairly new to Java and I need helop with a program that reads data from text files, places data on an array list and finally writes the contents of the array list to a text file. I have managed to get the read portion of the program to work properly and even displays the contents of the array list using a simple println statement. However, I am having trouble with the writing method of my program. Here is the code:
      import java.io.*;
      import java.util.*;
      
      public class RowMatcher {
      
           public static void main (String[] args) {
                readLines();
                writeLines();
           }
           
           public static void readLines() {
                
                ArrayList lines = new ArrayList();
                
                try {
                     BufferedReader inTT = new BufferedReader(new FileReader("C:\\TT_PCMAIN33.txt"));
                     BufferedReader inSW = new BufferedReader(new FileReader("C:\\SW_PCMAIN33.txt"));
                     
                     while (true) {
                          String TTline = inTT.readLine();
           
                          if (TTline == null) {
                               break;
                          }
                          
                          String[] TT1 = TTline.split(",");
                          lines.add(TT1[0]);
                     }
                     
                     while (true) {
                          String SWline = inSW.readLine();
           
                          if (SWline == null) {
                               break;
                          }
                          
                          String[] SW1 = SWline.split(",");
                          
                          if (lines.contains(SW1[0])) {
                               lines.remove(SW1[0]);
                          }
                     }
                     
                     inTT.close();
                     inSW.close();
                     
                     System.out.println(lines);
                }
                
                catch (IOException except) {
                     except.printStackTrace();
                }
           }
           
           public static void writeLines() {
                try
                {
                     Writer output = null;
                    String text = lines;
                    File file = new File("C:\\Output.txt");
                    output = new BufferedWriter(new FileWriter(file));
                    output.write(text);
                    output.close();
                }
                catch( IOException e )
                {
                   e.printStackTrace();
                }
                
                System.out.println("ArrayList has been written to output text file");
           }
      }
      At first I get an error saying the variable 'lines' cannot be resolved under the writeLines method. I am not sure how to reference it from the readLines method and I am also not sure if this is the proper way to write the contents of an array list to a text file. Any help would be really appreciated.

      Thanks
        • 1. Re: Writing an contents from an ArrayList to a text file
          800351
          Use for loop and do:
          out.write(lines.get(i));
          You should close the out after completeing the output.
          • 2. Re: Writing an contents from an ArrayList to a text file
            807603
            OK, now I got this:
            import java.io.*;
            import java.util.*;
            
            public class RowMatcher {
            
                 public static void main (String[] args) {
                      readLines();
                      writeLines();
                 }
                 
                 public static void readLines() {
                      
                      ArrayList lines = new ArrayList();
                      
                      try {
                           BufferedReader inTT = new BufferedReader(new FileReader("C:\\TT_PCMAIN33.txt"));
                           BufferedReader inSW = new BufferedReader(new FileReader("C:\\SW_PCMAIN33.txt"));
                           
                           while (true) {
                                String TTline = inTT.readLine();
                 
                                if (TTline == null) {
                                     break;
                                }
                                
                                String[] TT1 = TTline.split(",");
                                lines.add(TT1[0]);
                           }
                           
                           while (true) {
                                String SWline = inSW.readLine();
                 
                                if (SWline == null) {
                                     break;
                                }
                                
                                String[] SW1 = SWline.split(",");
                                
                                if (lines.contains(SW1[0])) {
                                     lines.remove(SW1[0]);
                                }
                           }
                           
                           inTT.close();
                           inSW.close();
                           
                           System.out.println(lines);
                      }
                      
                      catch (IOException except) {
                           except.printStackTrace();
                      }
                 }
                 
                 public static void writeLines() {
                      try
                      {
                           Writer output = null;
                          File file = new File("C:\\Output.txt");
                          output = new BufferedWriter(new FileWriter(file));
                          
                          for (int i;i<lines.size();i++) {
                                output.write(lines.get(i));
                          }
                          output.close();
                      }
                      catch( IOException e )
                      {
                         e.printStackTrace();
                      }
                      
                      System.out.println("ArrayList has been written to output text file");
                 }
            }
            However, the IDE is throwing me an error saying the 'lines' variables under the writeLines method cannot be resolved. I need to access the variable from outside for this. I am not sure how, any help?

            Thanks
            • 3. Re: Writing an contents from an ArrayList to a text file
              800351
              However, the IDE is throwing me an error saying the 'lines' variables under the writeLines method cannot be resolved. I need to access the variable from outside for this. I am not sure how, any help?
              Currently, your lines variable is a local variable of other method. Your wirteLines() method can't access it. Make your lines variable a class member == class global.
              • 4. Re: Writing an contents from an ArrayList to a text file
                807603
                OK, I have fixed the variable access problem by declaring it elsewhere. However I got another problem here:
                     public static void writeLines(ArrayList lines) {
                          try
                          {
                               Writer output = null;
                              File file = new File("C:\\Output.txt");
                              output = new BufferedWriter(new FileWriter(file));
                              
                              for (int i;i<lines.size();i++) {
                                    output.*write*(lines.get(i));
                              }
                              
                              output.close();
                          }
                          catch( IOException e )
                          {
                             e.printStackTrace();
                          }
                          
                          //System.out.println("ArrayList has been written to output text file");
                          //System.out.println(lines);
                     }
                The IDE is throwing me this error:
                "The method write(int) in the type Writer is not applicable for the arguments (Object)"

                I am not sure why this error is being shown. Any ideas on how to fix this? Thanks
                • 5. Re: Writing an contents from an ArrayList to a text file
                  800351
                  Do like this:
                  output.write((String)lines.get(i));
                  // or, output.write((String)(lines.get(i)));
                  Read the API documentation before using any class in the core library.