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

java中怎样从文件中读取数据?,第1张

分为读字节,读字符两种读法\x0d\x0a◎◎◎FileInputStream 字节输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0a\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("G:\\just for fun\\xiangwei.txt")\x0d\x0a\x0d\x0aFileInputStream fin=new FileInputStream(f)\x0d\x0a\x0d\x0abyte[] bs=new byte[1024]\x0d\x0a\x0d\x0aint count=0\x0d\x0awhile((count=fin.read(bs))>0)\x0d\x0a{\x0d\x0a\x0d\x0aString str=new String(bs,0,count)//反复定义新变量:每一次都 重新定义新变量,接收新读取的数据\x0d\x0a\x0d\x0aSystem.out.println(str)//反复输出新变量:每一次都 输出重新定义的新变量\x0d\x0a}\x0d\x0afin.close()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a◎◎◎FileReader 字符输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("H:\\just for fun\\xiangwei.txt")\x0d\x0a\x0d\x0aFileReader fre=new FileReader(f)\x0d\x0a\x0d\x0aBufferedReader bre=new BufferedReader(fre)\x0d\x0a\x0d\x0aString str=""\x0d\x0awhile((str=bre.readLine())!=null)//●判断最后一行不存在,为空\x0d\x0a{\x0d\x0aSystem.out.println(str)\x0d\x0a}\x0d\x0abre.close()\x0d\x0a fre.close()\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}

1.package txt

2.

3.import java.io.BufferedReader

4.import java.io.File

5.import java.io.FileInputStream

6.import java.io.InputStreamReader

7.

8./**

9. * 读取TXE数据

10. */

11.public class ReadTxtUtils {

12.public static void main(String arg[]) {

13.try {

14.String encoding = "GBK"// 字符编码(可解决中文乱码问题 )

15.File file = new File("c:/aa.txt")

16.if (file.isFile() &&file.exists()) {

17.InputStreamReader read = new InputStreamReader(

18.new FileInputStream(file), encoding)

19.BufferedReader bufferedReader = new BufferedReader(read)

20.String lineTXT = null

21.while ((lineTXT = bufferedReader.readLine()) != null) {

22. System.out.println(lineTXT.toString().trim())

23.}

24.read.close()

25.}else{

26.System.out.println("找不到指定的文件!")

27.}

28.} catch (Exception e) {

29.System.out.println("读取文件内容 *** 作出错")

30.e.printStackTrace()

31.}

32.}

33.}

java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?

?

List list=new ArrayList()

BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))))

String str=null

while((str=br.readLine())!=null)

{

list.add(new Integer(str))

}

Integer[] i=new Integer[list.size()]

list.toArray(i)

TXT文本中如据形如:

123

456

789

读入二维数组效果为:

temp[0][]={1,2,3}

temp[1][]={4,5,6}

temp[2][]={7,8,9}

import java.io.BufferedReader

import java.io.FileNotFoundException

import java.io.FileReader

import java.io.*

public class xx{

public static void main(String[]args){

String s

int[][]save=new int[3][3]

try{

BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"))

int i=0

while((s=in.readLine())!=null){

save[i][0]=Integer.parseInt(s.substring(0,1))

save[i][1]=Integer.parseInt(s.substring(1,2))

save[i][2]=Integer.parseInt(s.substring(2,3))

i++

}

}

catch(FileNotFoundException e){

e.printStackTrace()

}

catch(IOException e){

e.printStackTrace()

}

for(int i=0i<3i++)

{

for(int j=0j<3j++){

System.out.print(save[i][j])

}

System.out.println()

}

}

}

?

BufferedReader bf=new BufferedReader(new FileReader("Your file"))

String lineContent=null

int i = 0

int [][] temp = new int [3][]

while((lineContent=bf.readLine())!=null){

String [] str = lineContent.split("\\d")// 将 lineContent 按数字拆分

for(int j = 0j <str.length()j++){

int [i][j] = Integer.parseInt(str[j])

}

i++

}

scp|cs|ff|201101

这是d:\\a.txt的数据,与“|”分割取数据出来,保存在变量abcd里

import java.io.*

public class Test{

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

String a, b, c, d

StringBuffer sb = new StringBuffer()

BufferedReader br = new BufferedReader(new FileReader("d:\\a.txt"))

String s = br.readLine()

while(s != null){

sb.append(s)

s = br.readLine()

}

s = sb.toString()

String[] str = s.split("|")

a = str[0]

b = str[0]

c = str[0]

d = str[0]

}

}

Java中读取txt文件可以使用file类先创建一个对象,然后使用I/O *** 作,进行读取或者写入 *** 作,示例如下:\x0d\x0aimportjava.io.BufferedReader\x0d\x0aimportjava.io.File\x0d\x0aimportjava.io.FileInputStream\x0d\x0aimportjava.io.FileNotFoundException\x0d\x0aimportjava.io.FileOutputStream\x0d\x0aimportjava.io.IOException\x0d\x0aimportjava.io.InputStreamReader\x0d\x0aimportjava.io.PrintWriter\x0d\x0a\x0d\x0apublicclassdemo2{\x0d\x0aprivatestaticStringpath="f:/demo1.txt"\x0d\x0aprivatestaticFilefile\x0d\x0astatic{\x0d\x0afile=newFile(path)\x0d\x0aif(!file.exists()){\x0d\x0atry{\x0d\x0afile.createNewFile()\x0d\x0a}catch(IOExceptione){\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidmain(String[]args)throwsIOException{\x0d\x0aStudentstu=newStudent(1,"张三",90)\x0d\x0awriteDataToFile(file,stu)\x0d\x0areadDataFromFile(file)\x0d\x0a}\x0d\x0a\x0d\x0aprivatestaticvoidreadDataFromFile(Filefile)throwsIOException{\x0d\x0aBufferedReaderreader=newBufferedReader(newInputStreamReader(newFileInputStream(file)))\x0d\x0aStringstr=""\x0d\x0awhile((str=reader.readLine())!=null){\x0d\x0aString[]stuInfo=str.split(",")\x0d\x0aSystem.out.println("学号:"+stuInfo[0]+"姓名:"+stuInfo[1]+"score:"+stuInfo[2])\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aprivatestaticvoidwriteDataToFile(Filefile,Studentstu)throwsFileNotFoundException{\x0d\x0aPrintWriterout=newPrintWriter(newFileOutputStream(file,true))\x0d\x0aout.println(stu.toString())\x0d\x0aout.close()\x0d\x0a}\x0d\x0a}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11953585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存