本文目錄一覽:
- 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下,\r\n這兩個字元在一起時,表示一個換行。 // 但如果這兩個字元分開顯示時,會換兩次行。 // 因此,屏蔽掉\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();
}
}
}
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129386.html