java读取txt的简单介绍

博主:adminadmin 2022-12-01 02:37:09 53

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

本文目录一览:

java 读取txt

//注意:TXT中姓名,身份证号,出生日期,出境日期之间用一个英文半角空格隔开,就可以用以下代码处理。

import java.io.*;

public class Test {

public static void main(String[] args){

String s = new String();

try {

BufferedReader input = new BufferedReader(new FileReader("E:\\123.txt")); //读取流

while((s = input.readLine())!=null){ //判断是否读到了最后一行

String info[] = s.split(" ");

System.out.println("姓名:"+info[0]+"\n身份证号:"+info[1]+"\n出生日期:"+info[2]+"\n出境日期:"+info[3]);

}

input.close();

} catch (Exception e) {

}

}

}

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文件内容?

通常,可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可。

(1)JAVA 读取txt文件内容

(2)读取文件效果:

java怎么从txt文件中读取数据

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=0;i3;i++)

{

for(int j=0;j3;j++){

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 = 0; j str.length(); j++){

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

}

i++;

}

scp|cs|ff|201101

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

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这个类,这条路连接了你的磁盘,也就是文件处,和你的代码处,你需要一个外卖员帮你传输数据,就是流,实例流对象!然后就是读取,用read方法读,每次读取的数据,存进字节数组,然后传进String类的构造器,底层会给你把字节数组里的转为字符串,就读取出来了,

如何用java读取一个txt 文件内的内容并把它

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.Reader;

public class H {

/**

* 功能: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:\\20121012.txt";

// "res/";

readTxtFile(filePath);

}

}

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

The End

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