【问题标题】:Java read large text file with separatorJava读取带有分隔符的大文本文件
【发布时间】:2015-07-07 09:36:49
【问题描述】:

我正在尝试读取以下形式的大文本文件:

datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+

我想将文本文件中的这个字符串作为一个大的 java 字符串读取。这可能吗?我知道split方法的使用。

它可以逐行阅读,但我真正需要的是在“+”号处拆分这个长文本字符串。之后我想将它存储为一个数组,arraylist,list,...

谁能帮我解决这个问题?因为互联网上的每条信息都只是逐行读取文件。 提前致谢!

【问题讨论】:

标签: java file readline


【解决方案1】:

这是一种方法,需要注意的是您不能加载超过最大 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) {

          }
      }
  }

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      您应该能够获得长度为 Integer.MAX_VALUE 的字符串(根据 Java 规范,数组的最大大小始终为 2147483647 (231 - 1),String 类用于内部存储)或最大堆的一半大小(因为每个字符是两个字节),以较小者为准

      How many characters can a Java String have?

      【讨论】:

        【解决方案4】:

        您可以使用BufferedReader 或任何IO-classes 读取文件。假设您在testing.txt 文件中有该字符串,然后通过从文件中读取每一行,您可以将其拆分为分隔符(+)。并遍历数组并打印。

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

        【讨论】:

          【解决方案5】:

          在我看来,您的问题是您不想逐行读取文件。因此,请尝试分段阅读(例如每次 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);
              }
          }
          

          【讨论】:

            【解决方案6】:
            String inpStr = "datadfqsjmqfqs+dataqfsdqjsdgjheqf+qsdfklmhvqziolkdsfnqsdfmqdsnfqsdf+qsjfqsdfmsqdjkgfqdsfqdfsqdfqdfssdqdsfqdfsqdsfqdfsqdfs+qsfddkmgqjshfdfhsqdflmlkqsdfqdqdf+";
            
            String[] inpStrArr = inpStr.split("+");
            

            希望这是你需要的。

            【讨论】:

            • 好的,但是如何读取字符串呢?我认为会有溢出
            • 迭代循环并从数组中提取每个元素。 for (int i = 0; i
            • 应该是inpStr.split("\\+")
            • 不,我的意思是:如何将巨大的字符串从我的 .txt 文件读取到 java 字符串?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-01
            • 2023-03-31
            • 2019-12-07
            • 1970-01-01
            相关资源
            最近更新 更多