
import javaioFile;
import javaioRandomAccessFile;
/
2016年8月31日下午7:00:37
@author 3306 TODO 计算字节数
/
public class FileUtil {
public static void main(String[] args) {
String filePath = "d:/testtxt";// d盘必须存在testtxt文件
readEachLine(filePath);
}
/
打印文件每一行的字节数
@param filePath
文件路径
/
private static void readEachLine(String filePath) {
try {
File file = new File(filePath);
if (fileexists()) {// 文件存在
RandomAccessFile accessFile = new RandomAccessFile(file, "r");// 只赋予读的权限
String line = "";
long lineIndex = 1;
while (null != (line = accessFilereadLine())) {
Systemoutprintln("line" + (lineIndex++) + ": " + linegetBytes()length);// 打印行号和字节数
}
accessFileclose();
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
1package txt;
2
3import javaioBufferedReader;
4import javaioFile;
5import javaioFileInputStream;
6import javaioInputStreamReader;
7
8/
9 读取TXE数据
10 /
11public class ReadTxtUtils {
12 public static void main(String arg[]) {
13 try {
14 String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )
15 File file = new File("c:/aatxt");
16 if (fileisFile() && fileexists()) {
17 InputStreamReader read = new InputStreamReader(
18 new FileInputStream(file), encoding);
19 BufferedReader bufferedReader = new BufferedReader(read);
20 String lineTXT = null;
21 while ((lineTXT = bufferedReaderreadLine()) != null) {
22 Systemoutprintln(lineTXTtoString()trim());
23 }
24 readclose();
25 }else{
26 Systemoutprintln("找不到指定的文件!");
27 }
28 } catch (Exception e) {
29 Systemoutprintln("读取文件内容 *** 作出错");
30 eprintStackTrace();
31 }
32 }
33}
java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?
List list=new ArrayList();
BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("intxt"))));
String str=null;
while((str=brreadLine())!=null)
{
listadd(new Integer(str));
}
Integer[] i=new Integer[listsize()];
listtoArray(i);
TXT文本中如据形如:
123
456
789
读入二维数组效果为:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};
import javaioBufferedReader;
import javaioFileNotFoundException;
import javaioFileReader;
import javaio;
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:\\txttxt"));
int i=0;
while((s=inreadLine())!=null){
save[i][0]=IntegerparseInt(ssubstring(0,1));
save[i][1]=IntegerparseInt(ssubstring(1,2));
save[i][2]=IntegerparseInt(ssubstring(2,3));
i++;
}
}
catch(FileNotFoundException e){
eprintStackTrace();
}
catch(IOException e){
eprintStackTrace();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
Systemoutprint(save[i][j]);
}
Systemoutprintln();
}
}
}
或
BufferedReader bf=new BufferedReader(new FileReader("Your file"));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bfreadLine())!=null){
String [] str = lineContentsplit("\\d");// 将 lineContent 按数字拆分
for(int j = 0; j < strlength(); j++){
int [i][j] = IntegerparseInt(str[j]);
}
i++;
}
scp|cs|ff|201101
这是d:\\atxt的数据,与“|”分割取数据出来,保存在变量a;b;c;d里
import javaio;
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:\\atxt"));
String s = brreadLine();
while(s != null){
sbappend(s);
s = brreadLine();
}
s = sbtoString();
String[] str = ssplit("|");
a = str[0];
b = str[0];
c = str[0];
d = str[0];
}
}
import javaioBufferedReader;
import javaioFile;
import javaioFileReader;
public class ReadFiledata {
public static String txt2String(File file){
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = brreadLine())!=null){//使用readLine方法,一次读一行
resultappend(SystemlineSeparator()+s);
}
brclose();
}catch(Exception e){
eprintStackTrace();
}
return resulttoString();
}
public static void main(String[] args){
File file = new File("F:/cardtxt");//我的txt文本存放目录,根据自己的路径修改即可
Systemoutprintln(txt2String(file));
}
}
以上就是关于java读取txt文件每一行多少个字节全部的内容,包括:java读取txt文件每一行多少个字节、java怎么从txt文件中读取数据、java如何读取txt文本数据并以数组形式一行等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)