使用缓冲阅读器读取没有换行符的文件

我正在读取一个带有逗号分隔值的文件,当拆分为一个数组时,每行将有 10 个值。我希望文件有换行符,这样

line = bReader.readLine()

会给我每一行。但是我的文件没有换行符。相反,在第一组值之后有很多空格(准确地说是 465),然后下一行开始。

所以我上面的 readLine() 代码是一次性读取整个文件,因为没有换行符。请建议如何最好地有效地解决这种情况。


繁花不似锦
浏览 77回答 4
4回答

慕尼黑的夜晚无繁华

一种方法是在迭代读取之前用换行符“\n”替换文本中的 465 个空格。

神不在的星期二

我支持 Ninan 的回答:用换行符替换 465 个空格,然后运行您之前计划运行的功能。为了美观和可读性,我建议使用 Regex 的 Pattern 来替换空格而不是 long unreadable String.replace("                         ")。您的代码可能如下所示,但将 6 替换为 465: // arguments are passed using the text field below this editor  public static void main(String[] args)  {    String content = "DOG,CAT      MOUSE,CHEESE";    Pattern p = Pattern.compile("[ ]{6}",            Pattern.DOTALL | Pattern.CASE_INSENSITIVE);    String newString = p.matcher(content).replaceAll("\n");    System.out.println(newString);   }

月关宝盒

我的理解是你有一个没有正确换行符的平面 CSV 文件,每行应该有 10 个值。更新:1.(推荐)假设您尝试存储一行中的 10 个值,您可以使用带有 useDelimiter 的 Scanner 类来有效地解析 csv:&nbsp; &nbsp; public static void parseCsvWithScanner() throws IOException {&nbsp; &nbsp; Scanner scanner = new Scanner(new File("test.csv"));&nbsp; &nbsp; // set your delimiter for scanner, "," for csv&nbsp; &nbsp; scanner.useDelimiter(",");&nbsp; &nbsp; // storing 10 values as a "line"&nbsp; &nbsp; int LINE_LIMIT = 10;&nbsp; &nbsp; // implement your own data structure to store each value of CSV&nbsp; &nbsp; int[] tempLineArray = new int[LINE_LIMIT];&nbsp; &nbsp; int lineBreakCount = 0;&nbsp; &nbsp; while(scanner.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; // trim start and end spaces if there is any&nbsp; &nbsp; &nbsp; &nbsp; String temp = scanner.next().trim();&nbsp; &nbsp; &nbsp; &nbsp; tempLineArray[lineBreakCount++] = Integer.parseInt(temp);&nbsp; &nbsp; &nbsp; &nbsp; if (lineBreakCount == LINE_LIMIT) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace your own logic for handling the full array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<tempLineArray.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(tempLineArray[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } // end replace&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // resetting array and counter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempLineArray = new int[LINE_LIMIT];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineBreakCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; scanner.close();}或者使用 BufferedReader。如果通过替换您自己的逻辑存在内存问题,您可能不需要 ArrayList 来存储所有值。public static void parseCsv() throws IOException {&nbsp; &nbsp; BufferedReader br = new BufferedReader(new FileReader(file));&nbsp; &nbsp; // your delimiter&nbsp; &nbsp; char TOKEN = ',';&nbsp; &nbsp; // your requirement of storing 10 values for each "line"&nbsp; &nbsp; int LINE_LIMIT = 10;&nbsp; &nbsp; // tmp for storing from BufferedReader.read()&nbsp; &nbsp; int tmp;&nbsp; &nbsp; // a counter for line break&nbsp; &nbsp; int lineBreakCount = 0;&nbsp; &nbsp; // array for storing 10 values, assuming the values of CSV are integers&nbsp; &nbsp; int[] tempArray = new int[LINE_LIMIT];&nbsp; &nbsp; // storing tempArray of each line to ArrayList&nbsp; &nbsp; ArrayList<int[]> lineList = new ArrayList<>();&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; while((tmp = br.read()) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; if ((char)tmp == TOKEN) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lineBreakCount == LINE_LIMIT) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // your logic to handle the current "line" here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineList.add(tempArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // new "line"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempArray = new int[LINE_LIMIT];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineBreakCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // storing current value from buffer with trim of spaces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempArray[lineBreakCount] =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.parseInt(sb.toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineBreakCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // clear the buffer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.delete(0, sb.length());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add current char from BufferedReader if not delimiter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append((char)tmp);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; br.close();}

婷婷同学_

我的建议是读取文件 f1.txt 并通过删除所有空行和空格写入花药文件 f2.txt 然后读取 f2.txt 之类的东西FileReader fr = new FileReader("f1.txt");&nbsp;BufferedReader br = new BufferedReader(fr);&nbsp;FileWriter fw = new FileWriter("f2.txt");&nbsp;String line;&nbsp;while((line = br.readLine()) != null){&nbsp;line = line.trim(); // remove leading and trailing whitespaceif (!line.equals("")) // don't write out blank lines{&nbsp; &nbsp; fw.write(line, 0, line.length());}}然后尝试使用您的代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java