「java从文件提取数据」java写入数据到文件

博主:adminadmin 2022-11-27 17:46:07 47

今天给各位分享java从文件提取数据的知识,其中也会对java写入数据到文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中怎样从文件中读取数据?

分为读字节,读字符两种读法

◎◎◎FileInputStream 字节输入流读文件◎◎◎

public class Maintest {

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

File f=new File("G:\\just for fun\\xiangwei.txt");

FileInputStream fin=new FileInputStream(f);

byte[] bs=new byte[1024];

int count=0;

while((count=fin.read(bs))0)

{

String str=new String(bs,0,count); //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据

System.out.println(str); //反复输出新变量:每一次都 输出重新定义的新变量

}

fin.close();

}

}

◎◎◎FileReader 字符输入流读文件◎◎◎

public class Maintest {

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

File f=new File("H:\\just for fun\\xiangwei.txt");

FileReader fre=new FileReader(f);

BufferedReader bre=new BufferedReader(fre);

String str="";

while((str=bre.readLine())!=null) //●判断最后一行不存在,为空

{

System.out.println(str);

}

bre.close();

fre.close();

}

}

java中怎样取出文件中的数据

分为读字节,读字符两种读法

◎◎◎FileInputStream 字节输入流读文件◎◎◎

public class Maintest {

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

File f=new File("G:\\just for fun\\xiangwei.txt");

FileInputStream fin=new FileInputStream(f);

byte[] bs=new byte[1024];

int count=0;

while((count=fin.read(bs))0)

{

String str=new String(bs,0,count); //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据

System.out.println(str); //反复输出新变量:每一次都 输出重新定义的新变量

}

fin.close();

}

}

◎◎◎FileReader 字符输入流读文件◎◎◎

public class Maintest {

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

File f=new File("H:\\just for fun\\xiangwei.txt");

FileReader fre=new FileReader(f);

BufferedReader bre=new BufferedReader(fre);

String str="";

while((str=bre.readLine())!=null) //●判断最后一行不存在,为空

{

System.out.println(str);

}

bre.close();

fre.close();

}

}

Java中如何通过txt文件存储和取出数据?

Java中读取txt文件可以使用file类先创建一个对象,然后使用I/O操作,进行读取或者写入操作,示例如下:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class demo2 {

private static String path = "f:/demo1.txt";

private static File file;

static{

file = new File(path);

if(!file.exists()){

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

}

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

Student stu = new Student(1,"张三",90);

writeDataToFile(file,stu);

readDataFromFile(file);

}

private static void readDataFromFile(File file) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

String str = "";

while((str = reader.readLine())!=null){

String[] stuInfo = str.split(",");

System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);

}

}

private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {

PrintWriter out = new PrintWriter(new FileOutputStream(file, true));

out.println(stu.toString());

out.close();

}

}

java从文件提取数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java写入数据到文件、java从文件提取数据的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。