導航:首頁 > 編程語言 > java從文本中讀取

java從文本中讀取

發布時間:2022-10-02 20:33:55

① 簡述java中從文本文件中讀取數據的方法

好好看看java io

② 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從讀取的文本中 取出指定字元串

這個不難。讀取文本就當作讀取流的形式。

具體實現
//讀取一個文本的字元流
BufferedReader in = new BufferedReader(new FileReader("F:\\json.txt"));
String line = null;
//定義一個空字元串來接受讀到的字元串
String str="";
//循環把讀取到的字元賦給str
while((line = in.readLine())!=null)
{
str+=line;
}

System.out.println("str="+str);
//判斷str中是否有EFG子串,為true則說明有。進去if語句
if(str.contains("EFG")){
System.out.println("yes!");
//取得子串的初始位置
int i=str.indexOf("EFG");
//根據的要取的內容後多少字元+多少個
String strEFG=str.substring(i,i+3);
System.out.println("strEFG="+strEFG);
}

④ java 文本讀取

讀取文件用BufferedReader封裝
BufferedReader br = new BufferedReader(new FileInputStream(new File("d:/data.txt")));

然後str = br.readLine();
最後都拼成一個串。

用StringTokenizer st = new StringTokenizer(str , ",");按都好隔開;

之後操作就比較簡單了。好運 : [email protected]

⑤ java怎樣把一個文本內容讀取成字元串

java中可以使用Scanner來讀取文件的內容,首先先通過File創建一個文件,再通過Scanner的nextLine()方法讀取文本的內容。
具體代碼如下所示:
public class Demo {
public static void main(String[] args) {
File file = new File("C:/Users/hp/Desktop/data.txt");
Scanner scanner = null;
try {
scanner = new Scanner(file);
String str = null;
while (scanner.hasNextLine()) {
str += scanner.nextLine() + "\r\n";
}
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}
Scanner的主要功能是簡化文本掃描,這個類最實用的地方表現在獲取控制台輸入。

⑥ java :從一個文本文件中讀取所有字元,並輸出。求大神指教

public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("C:/Users/hsh/Desktop/a.wsdl");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String result = null;
while(bufferedReader.ready()){
result += bufferedReader.readLine();
}
System.out.println(result);
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("文件路徑不對,請核對!");
} catch (IOException e) {
System.out.println("輸入過程中意外地到達文件尾或流尾");
}
}

⑦ Java如何將文本文檔中的字元串讀取到字元串數組

使用RandomAccessFile先讀取一次計算行數,seek重置到文件頭部,再讀取每行,賦值給a數組。

importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.RandomAccessFile;
publicclassTest{
//此題目關鍵是根據文件內容確定二維數組的行數和列數
publicstaticvoidmain(String[]args){
RandomAccessFilereader=null;
try{
reader=newRandomAccessFile("test.txt","r");
intn=0;//行數
while(reader.readLine()!=null){//第一次按行讀取只為了計算行數
n++;
}
String[][]a=newString[n][];
reader.seek(0);//重置到文件頭部
intj;
Stringline;
String[]strs;
inti=0;
while((line=reader.readLine())!=null){//第二次按行讀取是真正的讀取數據
strs=line.split("");//把讀取到的一行數據以空格分割成子字元串數組
a[i]=newString[strs.length];//列數就是數組strs的大小,此句是逐行創建二維數組的列
for(j=0;j<strs.length;j++){
a[i][j]=strs[j];//逐行給二維數組的每一列賦值
}
i++;
}
for(i=0;i<n;i++){
for(j=0;j<a[i].length;j++){
System.out.println("a["+i+"]["+j+"]="+a[i][j]);
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
reader=null;
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}

運行結果如圖

⑧ java怎麼讀取文本文件中的所有字元

可以用文件流FileInputStream的方式讀取,如果文本文件太大了,不建議一次性往內存中讀,那往往會使之溢出。也可以一行行的讀取,用BufferReader讀,具體的實例都可以網路得到的。

⑨ java文本讀取

很簡單撒。利用HashMap不能重復的特點就可以了。

你先讀b.txt的數據到HashMap.

然後再讀a.txt到同一個Map

這個時候如果遇到重復的Key,value就會被覆蓋的。

就達到你的要求了撒。

參考代碼:

將A.txt B.txt 換成你自己的路勁就可以了。

packagea;

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Map.Entry;

publicclassTestReturn{
privatestaticHashMap<String,String>map=newHashMap<String,String>();
publicstaticvoidgetTxt(Filefile){
try{
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file));
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
map.put(lineTxt.split(",")[0],lineTxt.split(",")[1]);
}
read.close();
}
}catch(Exceptione){
//TODO:handleexception
e.printStackTrace();
}
}
publicstaticvoidwriteTxt(Filefile)throwsIOException
{
if(!file.exists())
file.createNewFile();
FileOutputStreamoutputStream=newFileOutputStream(file);
FileChannelchannel=outputStream.getChannel();
Stringcontent="";
Iteratoriter=map.entrySet().iterator();
while(iter.hasNext()){
Map.Entry<String,String>entry=(Entry<String,String>)iter.next();
content+=entry.getKey()+",";
content+=entry.getValue()+" ";
}
ByteBufferbuffer=ByteBuffer.allocate(content.getBytes().length);
buffer.put(content.getBytes());
buffer.flip();//此處必須要調用buffer的flip方法
channel.write(buffer);
channel.close();
outputStream.close();
}
publicstaticvoidmain(String[]args)throwsIOException{
//處理文件B
getTxt(newFile("d://B.txt"));
//處理文件A
getTxt(newFile("d://A.txt"));
//數寫入C
writeTxt(newFile("d://C.txt"));

}

}
閱讀全文

與java從文本中讀取相關的資料

熱點內容
程序員級別數學演算法邏輯 瀏覽:895
2k21公園怎麼換伺服器 瀏覽:724
php釋放資料庫連接 瀏覽:722
php網頁抓取工具 瀏覽:726
android設置對齊方式 瀏覽:23
linux創建網頁 瀏覽:280
凈化車間門演算法 瀏覽:934
安卓怎麼搞jpg 瀏覽:546
如來佛祖命令雷神去下界 瀏覽:856
新電腦管家下載好怎麼解壓 瀏覽:530
php獲取介面數據 瀏覽:766
最後的命令 瀏覽:921
如何添加手機app桌面快捷圖標 瀏覽:427
ui設計師與程序員 瀏覽:417
壽司pdf 瀏覽:828
pythonbg是什麼 瀏覽:248
c數值演算法程序大全 瀏覽:787
android整點報時 瀏覽:221
稀土pdf 瀏覽:536
單片機電子鎖 瀏覽:596