怎么用java使用BufferedInputStream读取文本文件
2个回答
展开全部
首先需要声明一个byte数组作为buffer,然后循环将文本内容循环读入到buffer中,并将buffer转换为字符串,打印到控制台。
/**
*
* @author outofmemory.cn
*/
public class Main {
/**
* 从文件中读取文本
*/
public void readFromFile(String filename) {
BufferedInputStream bufferedInput = null;
byte[] buffer = new byte[1024];
try {
//创建BufferedInputStream 对象
bufferedInput = new BufferedInputStream(new FileInputStream(filename));
int bytesRead = 0;
//从文件中按字节读取内容,到文件尾部时read方法将返回-1
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
//将读取的字节转为字符串对象
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//关闭 BufferedInputStream
try {
if (bufferedInput != null)
bufferedInput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* @param args 命令行参数
*/
public static void main(String[] args) {
new Main().readFromFile("myFile.txt");
}
}
/**
*
* @author outofmemory.cn
*/
public class Main {
/**
* 从文件中读取文本
*/
public void readFromFile(String filename) {
BufferedInputStream bufferedInput = null;
byte[] buffer = new byte[1024];
try {
//创建BufferedInputStream 对象
bufferedInput = new BufferedInputStream(new FileInputStream(filename));
int bytesRead = 0;
//从文件中按字节读取内容,到文件尾部时read方法将返回-1
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
//将读取的字节转为字符串对象
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//关闭 BufferedInputStream
try {
if (bufferedInput != null)
bufferedInput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* @param args 命令行参数
*/
public static void main(String[] args) {
new Main().readFromFile("myFile.txt");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedInputStreamTest {
public static void main(String[] args) throws IOException {
File file = new File("c:\\mm.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] contents = new byte[1024];
int byteRead = 0;
String strFileContents;
try {
while((byteRead = bis.read(contents)) != -1){
strFileContents = new String(contents,0,byteRead);
System.out.println(strFileContents);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bis.close();
}
}
望采纳
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedInputStreamTest {
public static void main(String[] args) throws IOException {
File file = new File("c:\\mm.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] contents = new byte[1024];
int byteRead = 0;
String strFileContents;
try {
while((byteRead = bis.read(contents)) != -1){
strFileContents = new String(contents,0,byteRead);
System.out.println(strFileContents);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bis.close();
}
}
望采纳
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询