简体   繁体   English

将字符串名称的一部分txt文件提取为空行

[英]extract a part of txt file from String name to an empty line

I want to extract a part of a text file beginning with a String name and ending with an empty line 我想提取文本文件的一部分,以字符串名称开头,以空行结尾

Here is what I tried: 这是我尝试过的:

public File CreateSubscripberFile(File file,String name) throws IOException
{

     FileWriter fw = new FileWriter(f);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while((line = br.readLine()) != null){
            if(line.equals(name))
            {
                while (((line = br.readLine()) != null) && (!(line.equals("\n"))))
                {
                             fw.write(line);
                             System.out.println(line);

                }   
            }      


        }
        br.close();
        fr.close();
        fw.close();

return f;
}   

but I get the original file as result! 但是我得到了原始文件!

Try to replace: 尝试更换:

(line = br.readLine())!=null)

with: 与:

(!(line = br.readLine()).equals(""))

null in this example would mean 'there is not more lines', and .equals(""), looks for empty line. 在此示例中,null表示“没有更多的行”,而.equals(“”)查找空行。

it's done by edit he code like this 这是通过编辑他的代码来完成的

    public File CreateSubscripberFile(File file,String name,String fname) throws IOException
{
    File f= new File(fname);
     FileWriter fw = new FileWriter(f);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null){
            if(line.equals(name))
            { 
                String line1 = br.readLine() ;
                while(line1 != null && !line1.isEmpty() )
                {

                    System.out.println(line1);
                    line1=br.readLine();
                }
            }
        }
        br.close();
        fw.close();
return f;
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储从txt文件到字符串java的行 - storing line from txt file to string java 如何从android应用程序中的txt文件中获取部分行 - How to get part of line from txt file in android app 如何从JSP中的“零件”类型文件中提取文件名? - How to extract file name from a “Part” type file in JSP? 用空行分隔.txt文件,然后复制到另一个.txt文件中 - Separate a .txt file by empty line and copy into another .txt file 缺少行吗? 从.txt文件读取字符串并将其放入数组(java) - Missing line? Reading string from .txt file and putting into arrays (java) 如何从带有int和String元素的txt文件中读取一行? - How to read a line from a txt file with int and String elements? 如果它们在同一行上,是否可以读取字符串和数字(来自 TXT 文件) - Is it possible to read a String and Numbers if they're on the same line (From a TXT file) 我如何从 java 7 中的 .txt 文件中提取主机名和主机请求的出现? - how can i extract host name and occurence of host request from .txt file in java 7? 从SDcard android的txt文件中提取段落 - extract paragraph from a txt file at SDcard android 通过扫描器将txt文件的1行分配给字符串 - Assigning 1 line of a txt file to a string via scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM