① 简述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"));
}
}