0

I'm trying to read in a file and modify the text, but I need to keep new lines when doing so. For example, if I were to read in a file that contained:

This is some text.

This is some more text.

It would just read in as

This is some text.This is some more text.

How do I keep that space? I think it has something to do with the /n escape character. I've seen using BufferReader and FileReader, but we haven't learned that in my class yet, so is there another way? What I've tried is something like this:

if (ch == 10)
{
   ch = '\n';
   fileOut.print(ch);
}

10 is the ASCII table code for a new line, so I thought Java could recognize it as that, but it doesn't.

weston
  • 54,145
  • 21
  • 145
  • 203
  • 5
    Escape characters begin with a backslash, not with a forward slash: it's `\n`, not `/n`. – BackSlash Mar 13 '17 at 16:51
  • 2
    Related: http://stackoverflow.com/questions/22021291/do-any-java-stream-input-libraries-preserve-line-ending-characters –  Mar 13 '17 at 16:51
  • It was a typo, my bad. It doesn't work even with a backslash. I don't want to use BufferedReader unless that's really the only way –  Mar 13 '17 at 16:58
  • 1
    Where is the code? – weston Mar 13 '17 at 17:06
  • 1
    If `ch == 10` then `ch = '\n'` (which you said is 10)? It doesn't make much sense to assign `ch` to what it already is. What OS was the file written with, Windows uses `\r\n`. Like @weston said, please include a [mcve] so that we can see what's going on. – Jonny Henly Mar 13 '17 at 17:25
  • 1
    *checks to see if @BackSlash is a bot that looks for BackSlash-ForwardSlash mistakes – weston Mar 13 '17 at 17:35

4 Answers4

1

In Java 8:

You can read lines using:

List<String> yourFileLines = Files.readAllLines(Paths.get("your_file"));

Then collect strings:

String collect = yourFileLines.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(" "));

  • This would load all lines to memory. It's much better to use the `Files::lines` method, which creates a stream directly. also, the OP said that they haven't learned `BufferedReader` yet, so one should assume that they haven't learned Java 8 streams, either. – RealSkeptic Mar 14 '17 at 08:48
0

/n should be \n

if (ch == 10)
{
   ch = '\n';
   fileOut.print(ch);
}
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
lakeIn231
  • 1,177
  • 3
  • 14
  • 34
0

The problem is that you (possibly) want to read your file a line at a time, and then you want to write it back a line at a time (keeping empty lines).

The following source does that, it reads the input file one line at a time, and writes it back one line at a time (keeping empty lines).

The only problem is ... it possibly changes the new line, maybe you are reading a unix file and write a dos file or vice-versa depending on the system you are running in and the source type of the file you a reading.

Keeping the original newline can introduce a lot complexity, read BufferedReader and PrintWriter api docs for more information.

public void process(File input , File output){
    try(InputStream in = new FileInputStream(input);
            OutputStream out = new FileOutputStream(output)){
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"),true);
        PrintWriter writer = new PrintWriter( new OutputStreamWriter(out,"utf-8"));
        String line=null;
        while((line=reader.readLine())!=null){
            String processed = proces(line);
            writer.println(processed);
        }

    } catch (IOException e) {
        // Some exception management
    }
}
public String proces(String line){
    return line;
}
minus
  • 2,646
  • 15
  • 18
-1

Is that a typo?

ch = '/n';

otherwise use

ch = '\n';
Luci
  • 1,278
  • 9
  • 17