導航:首頁 > 編程語言 > java批量閱讀text

java批量閱讀text

發布時間:2023-09-05 22:39:50

java編程:如何一行一行讀取TXT文檔

請看源代碼:import java.io.*;
public class Test{
public static void main(String args[])throws Exception{
File file = new File("D:\\Test.java");//Text文件
BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法回,一次讀一行答
System.out.println(s);
}
br.close();;
}
}

⑵ 各位大蝦,我想請教一下,java 讀取txt 文件時,是一次性讀入內存然後一行一行取呢還是一行一行來讀的

FileReader :把文件轉換為字元流讀入;

BufferedReader會一次性從物理流中讀取8k(默認數值,可以設置)位元組內容到內存

String line = br.readLine();然後一行一行取

具體看下面網址

⑶ java如何讀取一個txt文件的所有內容

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

public static void main(String[] args) throws IOException {

String fileContent = readFileContent("");

System.out.println(fileContent);
}

//參數string為你的文件名
private static String readFileContent(String fileName) throws IOException {

File file = new File(fileName);

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

String content = "";
StringBuilder sb = new StringBuilder();

while(content != null){
content = bf.readLine();

if(content == null){
break;
}

sb.append(content.trim());
}

bf.close();
return sb.toString();
}
}
求採納為滿意回答。

⑷ java中如何實現文件的批量讀取並提取部分內容寫入一個新文件。 單一文件讀取寫入參照補充

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testa
{
public static void main(String[] args)
{
//傳入參數為文件目錄
test("d:/a.txt");
}

public static void test(String filePath){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}

String []columnName = {"Id", "Name", "Languages", "Math", "English"}; //列名
int []courseIndexs = {2, 3, 4}; //課程對應的列
int i,j,index;
String line;
List> students = new ArrayList>();
//記錄Id和總值,用於排序
List> sortList = new ArrayList>();
try {
br.readLine(); //去掉第一行
while((line = br.readLine()) != null){
index = 0;
String []se = line.split(" ");
Map student = new HashMap();
for(i = 0; i < se.length; i++){
if("".equals(se[i])){
continue;
}
if(index >= columnName.length){
continue;
}
student.put(columnName[index], se[i]);
index++;
}
//計算平均值,總值
double total = 0;
for(j = 0; j < courseIndexs.length; j++){
total += Double.parseDouble((String) student.get(columnName[courseIndexs[j]]));
}
double average = total / courseIndexs.length;
//只取一位小數
average = Math.round(average * 10)/10;
student.put("Total", total);
student.put("Average", average);

Map sort = new HashMap();
sort.put("Id", student.get("Id"));
sort.put("Total", student.get("Total"));
sortList.add(sort);

students.add(student);
}
br.close();

//排序
for(i = 0; i < sortList.size(); i++){
for(j = i + 1; j < sortList.size(); j++){
if((Double)sortList.get(i).get("Total") < (Double)sortList.get(j).get("Total")){
Map temp = sortList.get(i);
sortList.set(i, sortList.get(j));
sortList.set(j, temp);
}
}
}
Map sortedId = new HashMap();
for(i = 0; i < sortList.size(); i++){
sortedId.put(sortList.get(i).get("Id"), i+1);
}

//設定序號
for(j = 0; j < students.size(); j++){
students.get(j).put("Order", sortedId.get(students.get(j).get("Id")));
}

//輸出(寫到原文件)
//PrintWriter pw = new PrintWriter(new File(filePath));
//輸出(寫到其他文件)
PrintWriter pw = new PrintWriter(new File("D:/b.txt"));

pw.println("Id\tName\tLan\tMath\tEnglish\tAverage\tTotal\tSort");
int cIndex;
for(i = 0; i < students.size(); i++){
Map st = students.get(i);
cIndex = 0;
pw.println(st.get(columnName[cIndex++]) + "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get(columnName[cIndex++])+ "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get("Total")
+ "\t" + st.get("Average")
+ "\t" + st.get("Order"));
}
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

⑸ java 多線程按行讀取txt 每個線程讀的內容不能重復 求demo

public class Test{
public static void main(String args[])throws Exception{
File file = new File("D:\\Test.java");//Text文件
BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法版,一次讀一行權
System.out.println(s);
}
br.close();;
}
}

⑹ Java編程Scanner讀取text文件問題

NoSuchElementException表示元素類型不匹配,你檢查下是不是輸入的數據不是double類型的或者是在其他地方出現了類型錯誤

閱讀全文

與java批量閱讀text相關的資料

熱點內容
U盤啟動載入資源文件失敗 瀏覽:459
怎樣把ug的pdf文件導入cad 瀏覽:148
c盤文件刪不了怎麼辦 瀏覽:248
win7無線網路禁用 瀏覽:455
如何把電腦文件夾圖片放置在一起 瀏覽:702
如何對訪談法進行數據分析 瀏覽:388
國土三調文件用什麼軟體什麼打開 瀏覽:651
數控銑床編程特點是什麼 瀏覽:119
飛鴿傳輸一次能傳多少文件 瀏覽:488
手機照片許可權怎麼設置密碼 瀏覽:341
es文件瀏覽器怎麼隱藏 瀏覽:649
刪除的文件為什麼恢復 瀏覽:314
血緣詛咒怎麼升級 瀏覽:604
文件分享到微信怎麼操作 瀏覽:393
tmx文件trados 瀏覽:704
大數據與會計選課怎麼選 瀏覽:684
網路的不穩定因素有哪些 瀏覽:950
獵豹瀏覽器javascript 瀏覽:723
哪個列印軟體保存數據時間長 瀏覽:873
ps合並後源文件 瀏覽:74

友情鏈接