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;i<3;i++) { for(int j=0;j<3;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文件入库,java读取TXT文件入库,java读取TXT文件入库,
java读取txt文件,可以以文件路径构造这个流,:FileInputStream fin = new FileInputStream("d:/test.txt"); 然后使用这个流直接读取到文件,再使用reader构造BufferedReader,按行读取一整行的文本,作为字符串返回...
使用java语言,通过调用FileReader类读取txt文件中的数据。资源是可直接实现的代码。
Java\读取TXT文件.txtJava\读取TXT文件.txt
java读取txt文件代码,轻松掌握java,不成问题
环境:eclipse3.7.2 jre1.6
java 创建txt_JAVA读取TXT文件、新建TXT文件、写入TXT文件
用java读取txt文件 并且格式化输出读取到的信息 用java读取excel的xml格式文件 对信息进行分类 并且格式化输出有效内容
java 读取txt文本文件中的数据并保存到数据库中源代码,假设txt已有格式,并以","分隔。其中的sql包需要自己去微软官网下载。
用Java程序实现对.txt文件的读写。txt文件可作为配置文件,以key-Value形式出现。
JAVA实现创建TXT文件 并执行读写操作
主要介绍了java读取txt文件并输出结果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有)
JAVA简单的读写文本文件的代码JAVA简单的读写文本文件的代码
JAVA读取TXT文件.pdf
读取一个文件加下的txt文件,读取文件中内容,自动录入数据库