本文目錄一覽:
Java 如何讀取txt文件的內容?
java讀取txt文件內容。可以作如下理解:
首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網絡了。接下來可以開始打電話了。
通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西
既然你使用了FileInputStream()。那麼對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據
解讀完成後要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用位元組碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。
package com.campu;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* @author Java團長
* H20121012.java
* 2017-10-29上午11:22:21
*/
public class H20121012 {
/**
* 功能: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:\\Apache\\htdocs\\res\\20121012.txt”;
// “res/”;
readTxtFile(filePath);
}
}
我有一個微信公眾號,經常會分享一些Java技術相關的乾貨文章,還有一些學習資源。
如果你需要的話,可以用微信搜索「Java團長」或者「javatuanzhang」關注。
java如何讀取txt文件內容?
通常,可以直接通過文件流來讀取txt文件的內容,但有時可能會出現亂碼!此時只要設置一下文件字符編碼即可。
(1)JAVA 讀取txt文件內容
(2)讀取文件效果:
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文件?
首先你要定義一條文件路線。就是實例File這個類,這條路連接了你的磁盤,也就是文件處,和你的代碼處,你需要一個外賣員幫你傳輸數據,就是流,實例流對象!然後就是讀取,用read方法讀,每次讀取的數據,存進位元組數組,然後傳進String類的構造器,底層會給你把位元組數組裡的轉為字符串,就讀取出來了,
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/193878.html