当前位置:首页 » 编程语言 » java行读取文件

java行读取文件

发布时间: 2022-07-08 09:53:05

java如何获取文件信息

File 类是对文件和文件夹的抽象,包含了对文件和文件夹的多种属性和操作方法。File类的常用方法如下表:

返回
方法
说明

String getName 获取文件名称
String getParent 获取文件的父路径字符串
String getPath 获取文件的相对路径字符串
String getAbsolutePath 获取文件的绝对路径字符串
boolean exists 判断文件或者文件夹是否存在
boolean isFile 判断是不是文件类型
boolean isDirectory 判断是不是文件夹类型
boolean delete 删除文件或文件夹,如果删除成功返回结果为true
boolean mkdir 创建文件夹,创建成功返回true
boolean setReadOnly 设置文件或文件夹的只读属性
long length 获取文件的长度
long lastModified 获取文件的最后修改时间
String[ ] list 获取文件夹中的文件和子文件夹的名称,并存放到字符串数组中

Ⅱ java 按行读取一个文件,存在字符串数组里,一个元素对应一行,再将这个数组按行输出到一个新的文件里

java 按行读取一个文件,存在字符串数组里,一个元素对应一行,再将这个数组按行输出到一个新的文件里,代码如下:

packagefoo;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;

importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.util.ArrayList;
importjava.util.List;

/**
*Helloworld!
*
*/
publicclassApp
{
publicstaticvoidmain(String[]args)
{
FilereadFile=newFile("D://home/a.txt");
InputStreamin=null;
InputStreamReaderir=null;
BufferedReaderbr=null;

OutputStreamout=null;
OutputStreamWriterow=null;
BufferedWriterbw=null;
try{
//用流读取文件
in=newBufferedInputStream(newFileInputStream(readFile));
//如果你文件已utf-8编码的就按这个编码来读取,不然又中文会读取到乱码
ir=newInputStreamReader(in,"utf-8");
br= newBufferedReader(ir);
Stringline="";
//定义集合一行一行存放
List<String>list=newArrayList<String>();
//一行一行读取
while((line=br.readLine())!=null){
System.out.println(line);
list.add(line);
}
//将集合转换成数组
String[]arr=list.toArray(newString[list.size()]);

//写入新文件
FilenewFile=newFile("D://home/b.txt");
if(!newFile.exists()){
newFile.createNewFile();
}

out=newBufferedOutputStream(newFileOutputStream(newFile));
//这里也可以给定编码写入新文件
ow=newOutputStreamWriter(out,"gb2312");
bw=newBufferedWriter(ow);
//遍历数组吧字符串写入新文件中
for(intx=0;x<arr.length;x++){
bw.write(arr[x]);
if(x!=arr.length-1){
//换行
bw.newLine();
}

}
//刷新该流的缓冲,这样才真正写入完整到新文件中
bw.flush();
}catch(Exceptione){

e.printStackTrace();
}finally{
//一定要关闭流,倒序关闭
try{
if(bw!=null){
bw.close();
}
if(ow!=null){
ow.close();
}
if(out!=null){
out.close();
}
if(br!=null){
br.close();
}
if(ir!=null){
ir.close();
}
if(in!=null){
in.close();
}
}catch(Exceptione2){

}

}

}
}

Ⅲ java怎么读入文件,并逐行输出

java读入文件,并逐行输出,先在D://home建立个文件夹,然后创建一个a.txt文件,然后编辑文件,文本编辑的编码是utf-8,然后用流逐行读取输出,如下:

importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStream;
importjava.io.InputStreamReader;

publicclassTestC{

publicstaticvoidmain(String[]args){
//获取要读取的文件
FilereadFile=newFile("D://home/a.txt");
//输入IO流声明
InputStreamin=null;
InputStreamReaderir=null;
BufferedReaderbr=null;

try{
//用流读取文件
in=newBufferedInputStream(newFileInputStream(readFile));
//如果你文件已utf-8编码的就按这个编码来读取,不然又中文会读取到乱码
ir=newInputStreamReader(in,"utf-8");
//字符输入流中读取文本,这样可以一行一行读取
br= newBufferedReader(ir);
Stringline="";

//一行一行读取
while((line=br.readLine())!=null){
System.out.println(line);

}

}catch(Exceptione){

e.printStackTrace();
}finally{
//一定要关闭流,倒序关闭
try{

if(br!=null){
br.close();
}
if(ir!=null){
ir.close();
}
if(in!=null){
in.close();
}
}catch(Exceptione2){

}

}

}


}

结果:
helloworld
您好
123456

Ⅳ java 如何读写文件

这个问题问的太泛泛了,读写文件,首先了解什么是输入流和输出流,什么是字节流,什么是字符流。然后创建流对象,调用其方法read or write File

Ⅳ Java怎么读取文件的任意行

java中没有指定行的方法。
只能挨个读取。如果你想快速提取。而且文件又不是很大的话,就用LIST吧。

Ⅵ Java中如何一行行地读文件

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
publicclassReadTest{
publicstaticvoidmain(String[]args){
//读控制台输入的文字!
BufferedReaderbr=null;
Stringstr=null;
try{
br=newBufferedReader(newInputStreamReader(System.in));
while(true){
str=br.readLine();
if(str.equals("886"))
break;
System.out.println(str);
}
//读文本文件..
br=newBufferedReader(newFileReader(newFile("C:\Users\Administrator\Desktop\地址.txt")));
for(str=br.readLine();str!=null;str=br.readLine()){
//打印你读的文本数据!
System.out.println(str);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}

核心就是:readLine()方法,一行一行的读!

Ⅶ 如何用java按行读取文本文件

File file = new File("文件地址");
Scanner scanner = new Scanner(file);
String lineContent = null;
while(scanner.hasNextLine()){//如果有下一行
lineContent = scanner.nextLine();//读取下一行内容
}
scanner.close();//关闭Scanner

Ⅷ JAVA中读取文件(二进制,字符)内容的几种方

JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用

public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}

/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}

} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* 随机读取文件内容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}

/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

Ⅸ java 按行读取txt文件的数字

可以通过Java的IO流实现txt文本的读取,然后用readline实现按行读取。具体代码如下:

packagetest;

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;

publicclassTest{
publicstaticdouble[]writeToDat(Stringpath){
Filefile=newFile(path);
Listlist=newArrayList();
double[]nums=null;
try{
BufferedReaderbw=newBufferedReader(newFileReader(file));
Stringline=null;
//因为不知道有几行数据,所以先存入list集合中
while((line=bw.readLine())!=null){
list.add(line);
}
bw.close();
}catch(IOExceptione){
e.printStackTrace();
}
//确定数组长度
nums=newdouble[list.size()];
for(inti=0;i<list.size();i++){
Strings=(String)list.get(i);
nums[i]=Double.parseDouble(s);
}
returnnums;
}
publicstaticvoidmain(String[]args){
Stringpath="d:/file4.txt";
double[]nums=writeToDat(path);
for(inti=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
}

Ⅹ java怎样读取大文件

读取文件行的标准方式是在内存中读取,Guava 和Apache Commons IO都提供了如下所示快速读取文件行的方法:

Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new File(path));

这种方法带来的问题是文件的所有行都被存放在内存中,当文件足够大时很快就会导致程序抛出OutOfMemoryError 异常。
例如:读取一个大约1G的文件:

@Testpublic void givenUsingGuava_whenIteratingAFile_thenWorks() throws IOException { String path = ... Files.readLines(new File(path), Charsets.UTF_8);}

这种方式开始时只占用很少的内存:(大约消耗了0Mb内存)

[main] INFO org.baelng.java.CoreJavaIoUnitTest - Total Memory: 128 Mb[main] INFO org.baelng.java.CoreJavaIoUnitTest - Free Memory: 116 Mb

然而,当文件全部读到内存中后,我们最后可以看到(大约消耗了2GB内存):

[main] INFO org.baelng.java.CoreJavaIoUnitTest - Total Memory: 2666 Mb[main] INFO org.baelng.java.CoreJavaIoUnitTest - Free Memory: 490 Mb

这意味这一过程大约耗费了2.1GB的内存——原因很简单:现在文件的所有行都被存储在内存中。
把文件所有的内容都放在内存中很快会耗尽可用内存——不论实际可用内存有多大,这点是显而易见的。
此外,我们通常不需要把文件的所有行一次性地放入内存中——相反,我们只需要遍历文件的每一行,然后做相应的处理,处理完之后把它扔掉。所以,这正是我们将要做的——通过行迭代,而不是把所有行都放在内存中。

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:237
java驼峰 发布:2025-02-02 09:13:26 浏览:652
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726