本文目錄一覽:
java如何讀取txt文件?
首先你要定義一條文件路線。就是實例File這個類,這條路連接了你的磁碟,也就是文件處,和你的代碼處,你需要一個外賣員幫你傳輸數據,就是流,實例流對象!然後就是讀取,用read方法讀,每次讀取的數據,存進位元組數組,然後傳進String類的構造器,底層會給你把位元組數組裡的轉為字元串,就讀取出來了,
java讀txt方法
1).按行讀取TXT文件
package zc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class readLine {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File(“C:/zc.txt”);
BufferedReader reader = null;
String tempString = null;
int line =1;
try {
System.out.println(“以行為單位讀取文件內容,一次讀一整行:”);
reader = new BufferedReader(new FileReader(file));
while ((tempString = reader.readLine()) != null) {
System.out.println(“Line”+ line + “:” +tempString);
line ++ ;
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2).按位元組讀取TXT文件
package zc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class readerFileByChars {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File(“c:/zc.txt”);
InputStream in = null;
byte[] tempByte = new byte[1024];
int byteread = 0;
try {
System.out.println(“以位元組為單位讀取文件內容,一次讀多個位元組:”);
in = new FileInputStream(file);
while ((byteread = in.read(tempByte)) != -1 ) {
System.out.write(tempByte, 0, byteread);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
java 讀取txt
//注意:TXT中姓名,身份證號,出生日期,出境日期之間用一個英文半形空格隔開,就可以用以下代碼處理。
import java.io.*;
public class Test {
public static void main(String[] args){
String s = new String();
try {
BufferedReader input = new BufferedReader(new FileReader(“E:\\123.txt”)); //讀取流
while((s = input.readLine())!=null){ //判斷是否讀到了最後一行
String info[] = s.split(” “);
System.out.println(“姓名:”+info[0]+”\n身份證號:”+info[1]+”\n出生日期:”+info[2]+”\n出境日期:”+info[3]);
}
input.close();
} catch (Exception e) {
}
}
}
java如何讀取txt文件內容?
通常,可以直接通過文件流來讀取txt文件的內容,但有時可能會出現亂碼!此時只要設置一下文件字元編碼即可。
(1)JAVA 讀取txt文件內容
(2)讀取文件效果:
如何用java讀取一個txt 文件內的內容並把它
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H {
/**
* 功能:Java讀取txt文件的內容
* 步驟:1:先獲得文件句柄
* 2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
* 3:讀取到輸入流後,需要讀取生成位元組流
* 4:一行一行的輸出。readline()。
* 備註:需要考慮的是異常情況
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding=”GBK”;
File file=new File(filePath);
if(file.isFile() file.exists()){ //判斷文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println(“找不到指定的文件”);
}
} catch (Exception e) {
System.out.println(“讀取文件內容出錯”);
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = “L:\\20121012.txt”;
// “res/”;
readTxtFile(filePath);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/183460.html