『壹』 一個文件夾下的多個txt文件,然後隨機讀取其中一個txt文件的內容(java代碼)
提供個思路:
1、把文件夾下所有txt文件的文件名,讀取List里。
2、生成一個隨機數,隨機的范圍是:0到List.size()-1。
3、用步驟2生產的隨機數取個文件名。List.get(隨機數變數)。
4、根據步驟3中取到的文件名,去讀取文件內容。
這樣就可以隨機讀取其中一個txt文件的內容了。
『貳』 java中怎樣讀取多個txt文件
java讀取txt文件內容。可以作如下理解:
1.首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網路了。接下來可以開始打電話了。
2.通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西
3.既然你使用了FileInputStream()。那麼對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據
4.解讀完成後要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用位元組碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。
下面看個代碼示例:
package com.campu;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* @author 碼農小江
* H20121012.java
* 2012-10-12下午11:40:21
*/
public class H20121012 {
/**
* 功能:Java讀取txt文件的內容
* 步驟:1:先獲得文件句柄
* 2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
* 3:讀取到輸入流後,需要讀取生成位元組流
* 4:一行一行的輸出。readline()。
* 備註:需要考慮的是異常情況
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判斷文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";
// "res/";
readTxtFile(filePath);
}
}
『叄』 java如何讀取txt文件內容
給你兩個方法,你可以看看;
//獲取值返回String文本
publicStringtxt2String(StringfilePath){
Filefile=newFile(filePath);
StringBuilderresult=newStringBuilder();
try{
BufferedReaderbr=newBufferedReader(newFileReader(file));//構造一個BufferedReader類來讀取文件
Strings=null;
while((s=br.readLine())!=null){//使用readLine方法,一次讀一行
result.append(s+System.lineSeparator());
}
br.close();
}catch(Exceptione){
e.printStackTrace();
}
returnresult.toString();
}
//獲取值不返回String文本
publicvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
『肆』 如何用java讀取txt文件的內容
import java.io.*; public class Read{ public static void main(String [] args) { try{
FileReader f = new FileReader("D:\\1.txt"); // 注意路徑這里可以有兩種表示方法一個種\\一種/就可以了 BufferedReader buf = new BufferedReader(f);
String s; while((s = buf.readLine() ) != null){ System.out.println(s); } f.close(); buf.close();
}catch(IOException e){} }}
『伍』 如何用java讀取一個txt 文件內的內容並把它
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H {
/**
* 功能:Java讀取txt文件的內容
* 步驟:1:先獲得文件句柄
* 2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
* 3:讀取到輸入流後,需要讀取生成位元組流
* 4:一行一行的輸出。readline()。
* 備註:需要考慮的是異常情況
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判斷文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\20121012.txt";
// "res/";
readTxtFile(filePath);
}
}
『陸』 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文件
讀取txt文件(一整個獲取)
『捌』 Java如何讀取txt文件的內容
publicclassMyword{
publicstaticvoidmain(String[]args)throwsIOException{
try{
FileInputStreamfile=newFileInputStream("e:/myText.txt");
BufferedInputStreamBfile=newBufferedInputStream(file);
byte[]b=newbyte[1024];
Strings="";
intbytesRead=0;
while((bytesRead=Bfile.read(b))!=-1){
s+=newString(b,0,bytesRead);
}
System.out.println(s);
}
}
『玖』 java讀txt方法
Java讀取txt文件和寫入txt文件寫Java程序時經常碰到要讀如txt或寫入txt文件的情況,但是由於要定義好多變數,經常記不住,每次都要查
『拾』 java如何讀取一個txt文件的所有內容
importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.io.Reader;
publicclassH{
/**
*功能:Java讀取txt文件的內容
*步驟:1:先獲得文件句柄
*2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
*3:讀取到輸入流後,需要讀取生成位元組流
*4:一行一行的輸出。readline()。
*備註:需要考慮的是異常情況
*@paramfilePath
*/
publicstaticvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargv[]){
StringfilePath="L:\20121012.txt";
//"res/";
readTxtFile(filePath);
}}