导航:首页 > 编程语言 > java读取txt内容

java读取txt内容

发布时间:2022-06-23 03:54:32

‘壹’ 一个文件夹下的多个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);
}}

阅读全文

与java读取txt内容相关的资料

热点内容
指数函数和对数函数的高精度快速算法 浏览:205
c预编译干什么 浏览:22
hp网络共享文件夹 浏览:363
程序员如何不被废 浏览:806
二进制流转pdf 浏览:916
php判断爬虫 浏览:571
960除24除4简便算法 浏览:786
关于解压英语翻译 浏览:565
python控制键盘右键 浏览:921
php没有libmysqldll 浏览:828
时政新闻app哪个好 浏览:906
手机已加密怎么办 浏览:201
安卓手机截屏怎么传到苹果 浏览:529
京管家app哪里下载 浏览:33
文件夹横向排列的竖向排列 浏览:453
51单片机驱动摄像头模块 浏览:689
政府文件加密没法转换 浏览:373
android判断栈顶 浏览:331
凭证软件源码 浏览:860
androidwebview滚动事件 浏览:11