本文目录一览:
- 1、用java从文件中读取二维数组
- 2、Java读取文件并存入数组后显示
- 3、JAVA读取文件内容并放入数组
- 4、java 以数组形式读取文件
- 5、Java读取TXT文件数据到数组
- 6、java读取txt文件写入数组
用java从文件中读取二维数组
纠正下,文件中是没有二维数组的概念的,也就是说必须将文件中的内容转换为二维数组中的内容。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//此处将读取到的内容根据需要的规则,写入到二维数组中即可
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
Java读取文件并存入数组后显示
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class FileToAry {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("c:\test.txt"));//使用BufferedReader 最大好处是可以按行读取,每次读取一行
int n = 0;//定义n
int m = 0;//定义m
int[] U = null;//定义数组
int[] S = null;//定义数组
int index = 0;//索引
String temp;//定义字符串,用于保存每行读取到的数据
while ((temp = br.readLine()) != null) {
int[] ary = aryChange(temp);//通过函数吧字符串数组解析成整数数组
if (index == 0) {
n = ary[0];
m = ary[1];
}
if (index == 1) {
U = ary;
}
if (index == 2) {
S = ary;
}
index++;
}
br.close();// 关闭输入流
// 测试输出
System.out.println("n=" + n + "tm=" + m + "n数组U=" + Arrays.toString(U) + "n数组S=" + Arrays.toString(S));
}
static int[] aryChange(String temp) {// 字符串数组解析成int数组
String[] ss = temp.trim().split("\s+");// .trim()可以去掉首尾多余的空格
// .split("\s+")
// 表示用正则表达式去匹配切割,\s+表示匹配一个或者以上的空白符
int[] ary = new int[ss.length];
for (int i = 0; i ary.length; i++) {
ary[i] = Integer.parseInt(ss[i]);// 解析数组的每一个元素
}
return ary;// 返回一个int数组
}
}
运行测试
n=5 m=10
数组U=[5, 13, 15, 5, 5]
数组S=[30, 20, 2, 14, 14]
JAVA读取文件内容并放入数组
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读一个字节 in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { System.out.write(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字节为单位读取文件内容,一次读多个字节:"); // 一次读多个字节 byte[] tempbytes = new byte[100]; int byteread = 0; in = new FileInputStream(fileName); ReadFromFile.showAvailableBytes(in); // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = in.read(tempbytes)) != -1) { System.out.write(tempbytes, 0, byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,rn这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。 if (((char) tempchar) != 'r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符为单位读取文件内容,一次读多个字节:"); // 一次读多个字符 char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); // 读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars)) != -1) { // 同样屏蔽掉r不显示 if ((charread == tempchars.length) (tempchars[tempchars.length - 1] != 'r')) { System.out.print(tempchars); } else { for (int i = 0; i charread; i++) { if (tempchars[i] == 'r') { continue; } else { System.out.print(tempchars[i]); } } } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 以行为单位读取文件,常用于读面向行的格式化文件 */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 随机读取文件内容 */ public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try { System.out.println("随机读取一段文件内容:"); // 打开一个随机访问文件流,按只读方式 randomFile = new RandomAccessFile(fileName, "r"); // 文件长度,字节数 long fileLength = randomFile.length(); // 读文件的起始位置 int beginIndex = (fileLength 4) ? 4 : 0; // 将读文件的开始位置移到beginIndex位置。 randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 // 将一次读取的字节数赋给byteread while ((byteread = randomFile.read(bytes)) != -1) { System.out.write(bytes, 0, byteread); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomFile != null) { try { randomFile.close(); } catch (IOException e1) { } } } } /** * 显示输入流中还剩的字节数 */ private static void showAvailableBytes(InputStream in) { try { System.out.println("当前字节输入流中的字节数为:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; ReadFromFile.readFileByBytes(fileName); ReadFromFile.readFileByChars(fileName); ReadFromFile.readFileByLines(fileName); ReadFromFile.readFileByRandomAccess(fileName); }}
java 以数组形式读取文件
public class Untitled1 {
public static void main(String[] args) {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("写上文件的路径")));
String str=br.readLine();//从文件里面读出一行
String result="";//定义你需要的字符串数组组成的字符串
while(str!=null){
result+=str;//每读一行就加到result里面去
str=br.readLine();//继续从文件里面读,知道str为空为止
}
String[] array=result.split(",");//讲得到的result按照,分开得到一个数组
for(int i=0;iarray.length;i++){
array[i]=array[i].trim();//去掉有可能读出来的空格
System.out.println("array["+i+"]=="+array[i]);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Java读取TXT文件数据到数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class FileToAry {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("c:\test.txt"));//使用BufferedReader 最大好处是可以按行读取,每次读取一行
int n = 0;//定义n
int m = 0;//定义m
int[] U = null;//定义数组
int[] S = null;//定义数组
int index = 0;//索引
String temp;//定义字符串,用于保存每行读取到的数据
while ((temp = br.readLine()) != null) {
int[] ary = aryChange(temp);//通过函数吧字符串数组解析成整数数组
if (index == 0) {
n = ary[0];
m = ary[1];
}
if (index == 1) {
U = ary;
}
if (index == 2) {
S = ary;
}
index++;
}
br.close();// 关闭输入流
// 测试输出
System.out.println("n=" + n + "tm=" + m + "n数组U=" + Arrays.toString(U) + "n数组S=" + Arrays.toString(S));
}
static int[] aryChange(String temp) {// 字符串数组解析成int数组
String[] ss = temp.trim().split("\s+");// .trim()可以去掉首尾多余的空格
// .split("\s+")
// 表示用正则表达式去匹配切割,\s+表示匹配一个或者以上的空白符
int[] ary = new int[ss.length];
for (int i = 0; i ary.length; i++) {
ary[i] = Integer.parseInt(ss[i]);// 解析数组的每一个元素
}
return ary;// 返回一个int数组
}
java读取txt文件写入数组
参考代码如下:
public class FileReaderForRead
{
public static void main(String [ ] args)
{
try {
System.out.println(System.in);
FileReader fileReader = new FileReader("D:\import_users.txt");
BufferedReader buf = new BufferedReader(fileReader);
int i = 0;
String bufToString = "";
String readLine = "";
String[] myArray = new String[500]; //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
while((readLine = buf.readLine()) != null){
myArray[i] = readLine;
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}