Reputation: 540
I have to create a little program for a text file like this:
hello_this_is_a_test_Example_
this line has to go up because it has a space at the beginning
this one too
this is the next line because there's no space at the start
this one has to connect with the first line
I hope you understand.
So at the end, it should save the formatted text in a file like this:
hello_this_is_a_test_Example_ this line has to go up because it has a space at the beginning this one too
this is the next line because there's no space at the start this one has to connect with te upper again
Basically, if the line has a space character at the start of each string, it has to connect with the top line. I already have the GUI to select both files just need the algorithms. Thank you in advance :D
I have this at the moment but it puts everything in one line. It's not right:
public class Engine {
public void Process(String fileIn,String fileOut) throws IOException{
System.out.println("[Info]--> Processign");
System.out.println("[Info]--> FileIn = " + fileIn);
System.out.println("[Info]--> FileOut = " + fileOut);
FileWriter Writer = new FileWriter(fileOut);
BufferedReader br = new BufferedReader(new FileReader(fileIn));
String line;
String modified;
while ((line = br.readLine()) != null) {
System.out.println(line);
Writer.write(line);
if(line.startsWith(" ")){
modified = line.replaceAll("\n ", " ");
Writer.write(modified);
}
}
br.close();
Writer.close();}
}
Upvotes: 2
Views: 796
Reputation: 37566
Try this:
String modified = yourString.replaceAll("\n ", " ");
Try something like this:
StringBuilder builder = new StringBuilder();
foreach (string line in Files.ReadAllLines(@"c:\myfile.txt"))
{
builder.append(line);
}
String modified = builder.toString().replaceAll("\n ", " ");
FileWriter fw = new FileWriter(@"c:\myfile.txt");
fw.write(modified);
fw.close();
Upvotes: 1