Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 格式化.txt文件中的文本_Java_File_Text - Fatal编程技术网

Java 格式化.txt文件中的文本

Java 格式化.txt文件中的文本,java,file,text,Java,File,Text,我必须为如下文本文件创建一个小程序: 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 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
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();
基本上,如果该行在每个字符串的开头都有一个空格字符,那么它必须与顶行连接。我已经有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();}    
    }
试试这个:

String modified = yourString.replaceAll("\n ", " ");
试着这样做:

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
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();

到目前为止,除了为此编写一个超级企业GUI之外,您还尝试了什么?自己尝试一些东西,如果你仍然遇到错误或者不理解一些东西,就回来。所以这里不是“做我的作业”的地方。要逐行阅读文本文件,请参阅。查看行是否以空格等开头的算法非常简单,您应该可以自己找到。感谢您的回答,但它不起作用我现在有这个::您需要在包含整个文件的sring上运行此操作。谢谢,但不要获取此部分:foreach(Files.ReadAllLines(@“c:\myfile.txt”)中的字符串行){builder.append(line);}读取字符串列表中的文件,并将每一行附加到生成器,以便从整个文件中生成一个字符串