亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用緩沖閱讀器讀取沒有換行符的文件

使用緩沖閱讀器讀取沒有換行符的文件

繁花不似錦 2022-06-15 10:10:39
我正在讀取一個帶有逗號分隔值的文件,當拆分為一個數組時,每行將有 10 個值。我希望文件有換行符,這樣line = bReader.readLine()會給我每一行。但是我的文件沒有換行符。相反,在第一組值之后有很多空格(準確地說是 465),然后下一行開始。所以我上面的 readLine() 代碼是一次性讀取整個文件,因為沒有換行符。請建議如何最好地有效地解決這種情況。
查看完整描述

4 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

一種方法是在迭代讀取之前用換行符“\n”替換文本中的 465 個空格。



查看完整回答
反對 回復 2022-06-15
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

我支持 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); 

  }


查看完整回答
反對 回復 2022-06-15
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

我的理解是你有一個沒有正確換行符的平面 CSV 文件,每行應該有 10 個值。


更新:1.(推薦)假設您嘗試存儲一行中的 10 個值,您可以使用帶有 useDelimiter 的 Scanner 類來有效地解析 csv:


    public static void parseCsvWithScanner() throws IOException {


    Scanner scanner = new Scanner(new File("test.csv"));


    // set your delimiter for scanner, "," for csv

    scanner.useDelimiter(",");


    // storing 10 values as a "line"

    int LINE_LIMIT = 10;


    // implement your own data structure to store each value of CSV

    int[] tempLineArray = new int[LINE_LIMIT];


    int lineBreakCount = 0;


    while(scanner.hasNext()) {


        // trim start and end spaces if there is any

        String temp = scanner.next().trim();

        tempLineArray[lineBreakCount++] = Integer.parseInt(temp);


        if (lineBreakCount == LINE_LIMIT) {


            // replace your own logic for handling the full array

            for(int i=0; i<tempLineArray.length; i++) {

                System.out.print(tempLineArray[i]);

            } // end replace


            // resetting array and counter

            tempLineArray = new int[LINE_LIMIT];

            lineBreakCount = 0;

        }

    }

    scanner.close();

}

或者使用 BufferedReader。如果通過替換您自己的邏輯存在內存問題,您可能不需要 ArrayList 來存儲所有值。


public static void parseCsv() throws IOException {

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

    // your delimiter

    char TOKEN = ',';

    // your requirement of storing 10 values for each "line"

    int LINE_LIMIT = 10;

    // tmp for storing from BufferedReader.read()

    int tmp;

    // a counter for line break

    int lineBreakCount = 0;

    // array for storing 10 values, assuming the values of CSV are integers

    int[] tempArray = new int[LINE_LIMIT];

    // storing tempArray of each line to ArrayList

    ArrayList<int[]> lineList = new ArrayList<>();

    StringBuilder sb = new StringBuilder();


    while((tmp = br.read()) != -1) {

        if ((char)tmp == TOKEN) {

            if (lineBreakCount == LINE_LIMIT) {

                // your logic to handle the current "line" here.

                lineList.add(tempArray);

                // new "line"

                tempArray = new int[LINE_LIMIT];

                lineBreakCount = 0;

            }

            // storing current value from buffer with trim of spaces

            tempArray[lineBreakCount] =

                    Integer.parseInt(sb.toString().trim());

            lineBreakCount++;

            // clear the buffer

            sb.delete(0, sb.length());

        }

        else {

            // add current char from BufferedReader if not delimiter

            sb.append((char)tmp);

        }

    }

    br.close();

}


查看完整回答
反對 回復 2022-06-15
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

我的建議是讀取文件 f1.txt 并通過刪除所有空行和空格寫入花藥文件 f2.txt 然后讀取 f2.txt 之類的東西


FileReader fr = new FileReader("f1.txt"); 

BufferedReader br = new BufferedReader(fr); 

FileWriter fw = new FileWriter("f2.txt"); 

String line;


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

line = line.trim(); // remove leading and trailing whitespace

if (!line.equals("")) // don't write out blank lines

{

    fw.write(line, 0, line.length());

}

}


然后嘗試使用您的代碼。


查看完整回答
反對 回復 2022-06-15
  • 4 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號