Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Print to File New line space question...

Hi, I am trying to write text to a file.  Each time this method is called I want the new text to go on the bottom of older text.  I am using "\n" newline space to try to do this.   However, this does not seem to work when I open the txt document, because all text is next to each other.  However, if I open the text file using a text editor like EditPlus, the "\n" are respected.  Can you please look at code below and possible fix this?

If possible, please provide some code.

Thanks,
G



************************* code ***********************
import java.io.*;

public class filePractice{
   
    public static void printFile(String filePath, String fileName, String contents) throws Exception{
        BufferedReader br = null;
        String input = null;
        FileOutputStream fos = null;
        try{          
               
                fos = new FileOutputStream(filePath + fileName, true);
                fos.write(("\n" +contents+"\n").getBytes()); //Does not print new line spaces...
                fos.flush();
                //fos.close();
           
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (br != null){
                br.close();
            }
            if (fos != null){
            fos.close();
            }
        }
    }

public static void main(String args[]) throws Exception{
        printFile("c:/temp/","test_09_16", "*** Testing 228");
    }


}
0
gen228
Asked:
gen228
  • 13
  • 11
1 Solution
 
CEHJCommented:
Try

PrintWriter out = new PrintWriter(new FileWriter(new File(filePath, fileName), true));
out.println(content);
0
 
gen228Author Commented:
Hi CEHJ, thanks for your comment, I am getting this compilation error:

symbol  : constructor FileWriter  (java.io.File,boolean)
location: class java.io.FileWriter
                PrintWriter out = new PrintWriter(new FileWriter(new File(filePath, fileName), true));

Thanks,
G.
0
 
CEHJCommented:
You must be using an old version of Java. Do it like this then

File f = new File(filePath, fileName);
PrintWriter out = new PrintWriter(new FileWriter(f.getName(), true));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gen228Author Commented:
Thanks CEHJ for you comment, but in notepad it is still doiing the same thing, it is not respecting the new line...  Does it work on your notepad?

Thanks,
G
0
 
CEHJCommented:
If you want to start on a completely new line, you can do

out.println();
out.println(contents);
0
 
CEHJCommented:
That should work - even in notepad ;-)
0
 
gen228Author Commented:
Thanks again CEHJ, I tried that but it still doesn't seem to work, I always get a box that I guess stands for the newline, is this only a Notepad thing, ?  because on more advanced editors like EditPlus, I don't get this problem.

Thanks,
G
0
 
CEHJCommented:
Yes, it is a Notepad thing, although i'm surprised it's happening. Try this

out.print("\r\n".getBytes());
out.print(contents);
out.print("\r\n".getBytes());
0
 
gen228Author Commented:
Thanks CEHJ, I really appreciate your effort.  Does this happen on your notepad also, could it be notepad does not understand the '\n' syntax?

Thanks,
G.
0
 
CEHJCommented:
The point is that *literally* '\n' is the newline character. Notepad can't cope with anything other than windows line feeds ("\r\n"). But the odd thing is that Java should convert '\n' to the correct platform linefeed automatically. I take it that my last code corrected the problem?
0
 
gen228Author Commented:
Hi CEHJ, I am still getting the same results, below is exactly what I am running right now:

package java_IO_Practice;

import java.io.*;

public class Copy{

public static void printFile(String filePath, String fileName, String contents) throws Exception{
        BufferedReader br = null;
        String input = null;
        FileOutputStream fos = null;
        try{          
               
                fos = new FileOutputStream(filePath + fileName, true);
                fos.write(("\n" +contents+"\n").getBytes()); //Does not print new line spaces...

                //PrintWriter out = new PrintWriter(new FileWriter(new File(filePath, fileName), true));
                //out.println(content);
                File f = new File(filePath, fileName);
                PrintWriter out = new PrintWriter(new FileWriter(f.getName(), true));
                out.print("\r\n".getBytes());
                out.print(contents);
                out.print("\r\n".getBytes());          

                fos.flush();
                //fos.close();
           
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (br != null){
                br.close();
            }
            if (fos != null){
            fos.close();
            }
        }
    }

public static void main(String args[]) throws Exception{
        printFile("c:/temp/","text_test.txt", "*** Testing 228");
    }


}
0
 
gen228Author Commented:
My apologies, CEHJ, the program has other lines that I didn't delete yet...I am looking at it right now.

G.
0
 
CEHJCommented:
This works fine for me in Notepad:


import java.io.*;

public class Copy {

      public static void printFile(String filePath, String fileName, String contents) throws Exception{
            File f = new File(filePath, fileName);
            PrintWriter out = new PrintWriter(new FileWriter(f.getName(), true));
            out.println(contents);
            out.close();
      }

      public static void main(String args[]) throws Exception{
            printFile(".","test.txt", "*** Testing 228");
      }


}
0
 
gen228Author Commented:
Hi CEHJ !, your solution is excellent, it works great!  My only last question is I am doing:

public static void main(String args[]) throws Exception{
          printFile("c:/temp/","cab.txt", "*** Testing 228");
}

However, the "cab.txt" file is not going to c:/temp directory, instead it is going to the same directory as my Copy java class.  Is the filePath being ignored?

-G
0
 
CEHJCommented:
C:\temp should already exist if you want to do that
0
 
sciuriwareCommented:
And these solutions work for JAVA 2 1.3

The earliest solution CEHJ gave is good for JAVA 2 1.4 and later (appending to a file).

;JOOP!
0
 
gen228Author Commented:
Thank you sciuriware for your comment

G.
0
 
gen228Author Commented:
Thanks CEHJ for all your comments and code.  I have c:/temp existing, but it still seems to want to go to only the immediate directory(directory of my java class and source).  Anyway, if you have any comments about this, please post !  Thanks again for your great answers !

Thanks again,
G.
0
 
CEHJCommented:
>>but it still seems to want to go to only the immediate directory(directory of my java class and source).  

Any difference if you do

PrintWriter out = new PrintWriter(new FileWriter(f.getName(), true));

?
0
 
gen228Author Commented:
Hi CEHJ, thank you for your continued help.... I have tried the above code and it still doesn't seem to take in the directory path...here is my code... I am using jdk 1.3.1   Thanks -G

import java.io.*;

public class Copy{

     public static void printFile(String filePath, String fileName, String contents) throws Exception{
          File f = new File(filePath, fileName);
         
          PrintWriter out = new PrintWriter(new FileWriter(f.getName(), true));


          out.println(f.getPath());
          out.println(contents);
          out.close();
     }

     public static void main(String args[]) throws Exception{
          printFile("c:/tempLogin/","login.txt", "*** NUMBER ONE ***");
     }


}
0
 
CEHJCommented:
LOL sorry. I meant to say

PrintWriter out = new PrintWriter(new FileWriter(f.getPath(), true));
0
 
gen228Author Commented:
LOL, oh my goodness, I didn't notice that either!  It works now!  Thanks, CEHJ for you great answers !  And thanks for continuing to help !  

Thanks again,
G.
0
 
gen228Author Commented:
BTW, if I open a new question in the future, is there any way to notify you? can I comment on this question link and you will get a notification?

Thanks,
G.
0
 
CEHJCommented:
:-)

>>BTW, if I open a new question in the future, is there any way to notify you?

I'm almost certainly going to be among the first to see it anyway ;-)
0
 
gen228Author Commented:
Thank you
-G
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now