简体   繁体   English

Java用分隔符读取大文本文件

[英]Java read large text file with separator

I'm trying to read a large text file in the form of: 我正在尝试读取以下形式的大文本文件:

datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+

I want to read this string in the text file as one big java String. 我想在文本文件中将此字符串作为一个大的Java字符串读取。 Is this possible? 这可能吗? I know the use of the split method. 我知道使用split方法。

It worked to read it line by line, but what I really need is to split this long text-string at the '+' sign. 它可以逐行读取它,但我真正需要的是在“ +”号处拆分此长文本字符串。 Afterwards I want to store it as an array, arraylist, list,... 之后,我想将其存储为数组,arraylist,列表,...

Can anyone help me with this? 谁能帮我这个? Because every information on the internet is just about reading a file line by line. 因为互联网上的所有信息都只是逐行读取文件。 Thanks in advance! 提前致谢!

String inpStr = "datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+";

String[] inpStrArr = inpStr.split("+");

Hope this is what you need. 希望这是您所需要的。

It seems to me like your problem is that you don't want to read the file line by line. 在我看来,您的问题似乎是您不想逐行读取文件。 So instead, try reading it in parts (say 20 characters each time and building your string): 因此,请尝试分批阅读(每次说20个字符并构建您的字符串):

char[] c = new char[20]; //best to save 20 as a final static somewhere

ArrayList<String> strings = new ArrayList<String>();
StringBuilder sb = new StringBuilder();

BufferedReader br = new BufferedReader(new FileReader(filename));

while (br.read(c) == 20) {

    String str = new String(c);

    if (str.contains("+") {

        String[] parts = str.split("\\+");
        sb.append(parts[0]);
        strings.add(sb.toString());

        //init new StringBuilder:
        sb = new StringBuilder();
        sb.add(parts[1]);

    } else {
        sb.append(str);
    }
}

You can read file using BufferedReader or any IO-classes.suppose you have that String in testing.txt file then by reading each line from file you can split it by separator ( + ). 您可以使用BufferedReader或任何IO类读取文件testing.txt假设您在testing.txt文件中具有该String,然后从文件中读取每一行,则可以用分隔符( + )进行分割。 and iterate over array and print. 并遍历数组并打印。

BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("C:\\testing.txt"));//file name with path
        while ((sCurrentLine = br.readLine()) != null) {
               String[] strArr = sCurrentLine.split("\\+");
               for(String str:strArr){
                    System.out.println(str);
                      }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller 您应该能够获得长度为Integer.MAX_VALUE的字符串(根据Java规范,总是2147483647(231-1),是数组的最大大小,String类用于内部存储)或最大堆大小的一半(因为每个字符为两个字节),以较小者为准

How many characters can a Java String have? 一个Java字符串可以有几个字符?

Try this one: 试试这个:

private static void readLongString(File file){
    ArrayList<String> list = new ArrayList<String>();
    StringBuilder builder = new StringBuilder();
    int r;
    try{
        InputStream in = new FileInputStream(file);
        Reader reader = new InputStreamReader(in);
            while ((r = reader.read()) != -1) {
                if(r=='+'){
                    list.add(builder.toString());
                    builder = new StringBuilder();
                }
                builder.append(r);
            }
    }catch (IOException ex){
        ex.printStackTrace();
    }
    for(String a: list){
        System.out.println(a);
    }
}

Here is one way, caveat being you can't load more than the max int size (roughly one GB) 这是一种方法,请注意,您不能加载超过最大int大小(大约1 GB)的文件

  FileReader fr=null; try { File f=new File("your_file_path"); fr=new FileReader(f); char[] chars=new char[(int)f.length()]; fr.read(chars); String s=new String(chars); //parse your string here } catch (Exception e) { e.printStackTrace(); }finally { if(fr!=null){ try { fr.close(); } catch (IOException e) { } } } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM