11 Replies Latest reply: Apr 2, 2008 1:45 AM by camickr RSS

    Writing to txt file problem

    807591
      hi im having a bit of trouble with this output
      it writes to a file the directory it is currently in

      the problem is that it writes left to right meaning anything in that directory gets output
      e.g.
      saveto1.txt~ test3.txt tester.java user2.txt

      but what i would really like it to do is write up and down
      e.g.

      saveto1.txt
      test3.txt
      tester.java
      user2.txt

      is there a way to this thanks for help
      int ch;
                //Stream to write file
                FileOutputStream fout;          
                try { Process myProcess = Runtime.getRuntime().exec("dir"); 
                 InputStreamReader myIStreamReader = new InputStreamReader(myProcess.getInputStream()); 
                 fout = new FileOutputStream ("user2.txt");
                 while ((ch = myIStreamReader.read()) != -1) { new PrintStream(fout).print((char)ch); } } 
                      catch (IOException anIOException) { System.out.println(anIOException); }
        • 1. Re: Writing to txt file problem
          camickr
          while ((ch = myIStreamReader.read()) != -1) { new PrintStream(fout).print((char)ch); } }
          Whitespace is your friend, learn how to use it. There is no need to code everything on one line.

          Use a BufferReader and Writer. Then you can read one line at a time and when you write it out you can also write out the newline String. Read the API for the appropriate methods.
          • 2. Re: Writing to txt file problem
            807591
            i could use scanner to read it up and down

            but im wanting to write a txt file in that way?
            any help would be appriciated
            • 3. Re: Writing to txt file problem
              807591
              someone please help me with this?
              • 4. Re: Writing to txt file problem
                807591
                Is this an April joke or a real problem?
                • 5. Re: Writing to txt file problem
                  807591
                  huh of course its a real problem
                  why would it be a joke?
                  im stuck
                  • 6. Re: Writing to txt file problem
                    807591
                    Don't use exec at all for this.
                    Use java.io.File.listFiles and related methods.

                    And for God's sake stop cramming all your code together.

                    And for all that is holy, don't create a new PrintStream for every character.

                    Seriously, this is an April Fool's Day joke, isn't it?
                    • 7. Re: Writing to txt file problem
                      807591
                      Sorry about asking whether it is an April Fool's Day joke. But in your code are so many things that don't make sense that I had to ask this ...

                      For your convenience here is a sample that does what I assume you are looking for:
                        public static void main(String[] arg)
                          {
                          File dir = new File(".");
                          File[] files = dir.listFiles();
                          FileOutputStream ostream;
                          
                          try
                            {
                            ostream = new FileOutputStream("user2.txt");
                            for (int i = 0; i < files.length; i++)
                              {
                              if (files.isFile())
                      {
                      ostream.write(files[i].getName().getBytes());
                      ostream.write("\r\n".getBytes());
                      }
                      }
                      }
                      catch(IOException e)
                      {
                      System.out.println(e);
                      }
                      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                      • 8. Re: Writing to txt file problem
                        807591
                        By the way. If you prefer a PrintStream, then the solution must be slightly modified:
                          public static void main(String[] arg)
                            {
                            File dir = new File(".");
                            File[] files = dir.listFiles();
                            FileOutputStream ostream;
                            PrintStream pstream; 
                            
                            try
                              {
                              ostream = new FileOutputStream("user2.txt");
                              pstream = new PrintStream(ostream);
                              for (int i = 0; i < files.length; i++)
                                {
                                if (files.isFile())
                        {
                        pstream.print(files[i].getName());
                        pstream.println();
                        }
                        }
                        }
                        catch(IOException e)
                        {
                        System.out.println(e);
                        }
                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                        • 9. Re: Writing to txt file problem
                          807591
                          well first of all its the 2nd of April over here i live in New Zealand.
                          and i crammed it all together cause i think all \\comments wrnt needed

                          its not necessarily that i need to use the dir i was just using that as an example
                          i was trying to get it to write to txt
                          like this:
                          hello.txt
                          hello2.txt

                          not like this:
                          hello.txt hello2.txt

                          the main thing i want to do is learn how to do this?
                          • 10. Re: Writing to txt file problem
                            807591
                            OK - All you need is in the example above. Load it in Eclipse and do a single step debug. So you can see what happens.

                            Here the important parts:

                            1. Create a single PrintStream for the whole output
                            2. Add content to the PrintStream with the "print" method
                            3. Start a new line in the output file with the "println" method
                            4. At the end, you should close the stream with the "close" method (This is missing in the example)

                            5. Buy a good book about Learning Java
                            6. Read this book
                            7. Try all examples in the book
                            • 11. Re: Writing to txt file problem
                              camickr
                              and i crammed it all together cause i think all comments wrnt needed
                              White space makes your code readable.
                              the main thing i want to do is learn how to do this?
                              I already gave a suggestion in my first reply.

                              You have yet to post the code where you tried my suggestion so I have no further help to offer.

                              All you have to do is search the forum for example code using the classes I suggested. Or you can always read the Java tutorial on I/O.