0

I have a StringBuffer that contains what used to be multiple lines from a file, all merged together based on a pattern of larger lines for comparisons. After changes have been made, I basically need to undo merging the lines together to re-write to a new StringBuffer to re-write the file as it originally was.

So for example, the file contains a line like this (all on one line):

'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'

I need that line to be re-written to multiple lines (split wherever there is the string "macro(continued)"):

macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'

If it helps, here is my code snippet that does the first part of taking the original file where it is in multiple lines and combines them to groups and writes to a new file:

    StringBuffer sb = new StringBuffer();
    Buffer = new FileReader(C:/original.txt);
    BufferedReader User_File = new BufferedReader(Buffer);
    String Line_of_Text;
    while((Line_of_Text = User_File.readLine()) != null){
        if(Line_of_Text.startsWith("macro ")){
            sb.append("\n"+Line_of_Text);
        }else if(Line_of_Text.startsWith("macro(continued)")){
            sb.append(Line_of_Text);
        }else{
            sb.append(Line_of_Text+"\n");
        }
    }
    BufferedWriter OutFile = new BufferedWriter(new FileWriter("C:/newFile.txt"));
    OutFile.write(sb.toString());
    OutFile.flush();
    OutFile.close();

I have been stuck on a way to do this for a while, any suggestions/ideas?

5 Answers5

0

I hope that the split() and contains() methods from String class can help you.

Ref: How to split a string in Java

Community
  • 1
  • 1
0

The problem is that you have a big String with no lines. You can test what you have above your line if(Line_of_Text.startsWith("macro ")){ by inputting System.out.println(Line_of_Text);. That what you can see what is going wrong.

I'd change your appending code to:

String Line_of_Text = null;
while ((Line_of_Text = User_File.readLine()) != null) {
    Line_of_Text = User_File.readLine();
    String[] splitted = Line_of_Text.split("macro ");
    for (String part : splitted) {
        sb.append(part+"\n");
    }
}

This utilizes the .split() method to split your String into an array of String. You can use the items in that array to append them.

EDIT: I see you have multiple delimiters. .split() takes a regex delimiter, so you can try: Line_of_Text.split("macro|macro\\(continued\\)");

Joetjah
  • 6,292
  • 8
  • 55
  • 90
0

You can use String#split on a given token to split your String.

Example

String test = "'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'";
//                       // splits into String array, by
//                       //     | followed by...
//                       //     |  | literal text
//                       //     |  |   | double-escape on parenthesis, as
//                       //     |  |   | this is a regex pattern
String[] splitted = test.split("(?=macro\\(continued\\))");
System.out.println("Raw:\r\n");
// prints the raw array (hard to read, as inline)
System.out.println(Arrays.toString(splitted)); 
// creates a list from the array and print each element one per line, trimmed
List<String> splittedList = Arrays.asList(splitted);
System.out.println("\r\nNice:\r\n");
for (String s: splittedList) {
    System.out.println(s.trim());
}

Output

Raw:

['macro NDD89 Start;Load; shortDescription; , macro(continued) stepone;steptwo,do stuff , macro(continued) stepthree;more steps; do stuff , macro(continued) finalstep']

Nice:

'macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'
Mena
  • 47,782
  • 11
  • 87
  • 106
0

The simplest solution!

String x =oneLine.replace(" macro", "\nmacro");
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
0

Quick fix to QuakCore's answer.

String breaker = "macro(continued)";
String x =oneLine.replace(" "+breaker, "\n"+breaker);
McNultyyy
  • 992
  • 7
  • 12