1

enter image description here

When I am writing text in this txt file, there either is no space between the new string and the old existing string, or there is extra lines, which messes up my other algorithms.


    public String writeStudent(String file, String name)
        {

            String txt = "";
            //set through put method

            try(FileWriter fw = new FileWriter(file + ".txt", true);
                BufferedWriter bw = new BufferedWriter(fw);

                    PrintWriter out = new PrintWriter(bw))
            {

                out.println(name + "\r\n");


                //save userinput into class1.txt

                txt ="added: " + name;

             }

            catch(IOException e)
            {
                System.out.println("error");
                e.printStackTrace();
                // detact error
            }
    return txt;
    }

This is the code I am using to writing in txt, using (name + "\r\n") gives me extra empty lines.

Nagaraj Kandoor
  • 305
  • 5
  • 18
Steven Oh
  • 382
  • 3
  • 14

2 Answers2

1

How about use BufferedWriter instead of PrintWriter?

It's my sample code. please try test below code.

import java.io.*;

public class Stackoverflow {
    public static void main(String[] args) {
        File file = new File("C:\\test.txt");
        OutputStream outputStream = null;
        Writer writer = null;
        BufferedWriter bufferedWriter = null;

        try {
            outputStream = new FileOutputStream(file);
            writer = new OutputStreamWriter(outputStream);
            bufferedWriter = new BufferedWriter(writer);

            bufferedWriter.write("Hello");
            bufferedWriter.write("\r\n");
            bufferedWriter.write("\r\n");
            bufferedWriter.write("Bye");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (Exception ignore) {

                }
            }

            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception ignore) {

                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception ignore) {

                }
            }
        }

    }
}

output

Hello

Bye
Han
  • 263
  • 1
  • 8
  • It worked but it also deleted the previous contents in the file. @Han – Steven Oh Apr 28 '20 at 04:25
  • If you don't want to delete previous contents then read the file first, and save the contents to a StringBuffer(or StringBuilder, String any type what you want). After read all contents append what you want to write. reference : https://www.tutorialspoint.com/java/stringbuffer_append.htm – Han Apr 28 '20 at 04:41
1

The problem is the println function automatically adds new line at end of input string.

out.println(name + "\r\n"); Is effectively the same as out.print(name + "\r\n\r\n");

Lastly you need to think about if new line needs to be before or after your student name.

The solution is to simply use print instead of println and add a new line before the student name

For example.

Given existing text file

John Doe

and you want a new name to be added as

John Doe
Jane Doe

The newline is actually before your name input. Meaning you should use something like out.print("\r\n" + name);

alan7678
  • 2,223
  • 17
  • 29