我必须为这样的文本文件创建一个小程序:
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
我希望你理解。
所以最后,它应该将格式化的文本保存在这样的文件中:
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
基本上,如果行在每个字符串的开头都有空格字符,则必须与顶行连接。我已经有GUI来选择两个文件只需要算法。提前谢谢你:D
我现在有这个,但它把所有东西放在一行。这不对:
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();}
}
答案 0 :(得分:1)
试试这个:
String modified = yourString.replaceAll("\n ", " ");
尝试这样的事情:
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();