java读取txt文件|java如何读取一个txt文件的所有内容

|

『壹』 java读取txt文件

importjava.io.File;publicclassTest{publicstaticvoidmain(String[]args){try{Filefile=newFile("info.txt");newRead().readFile(file);}catch(Exceptione){e.printStackTrace();}}}importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.util.ArrayList;publicclassRead{publicvoidreadFile(Filefile){ArrayList<String>arrayList=newArrayList<>();try{BufferedReaderbufferedReader=newBufferedReader(newFileReader(file));inti=1;Stringline=null;Stringperson="";while((line=bufferedReader.readLine())!=null){String[]strings=line.split("\s+");for(Strings:strings){if(i!=4){person+=s+",";}else{person+=s;arrayList.add(person);person="";i=0;}i++;}}System.out.println("{");for(i=0;i<arrayList.size();i++){Strings=arrayList.get(i);if(i!=arrayList.size()-1)System.out.print("["+s+"];");elseSystem.out.print("["+s+"]");}System.out.println("}");}catch(Exceptione){e.printStackTrace();}}}

『贰』 如何用java读取txt文件

用java读取txt文件:public String read(String path) throws Exception { //读File f = new File(path);FileInputStream input = new FileInputStream(f);BufferedInputStream buf=new BufferedInputStream(input);byte[] b=new byte[(int) f.length()];input.read(b); input.close();return new String(b);}public static void writeFileByByte(String path,String strs,boolean a) throws Exception{ //写 File f1=new File(path); FileOutputStream out=new FileOutputStream(f1,a); byte[] b=strs.getBytes(); out.write(b); out.close(); }也可以参考JAVA IO。public class ReadTxtFile { public static void main(String[] args) throws Exception { File file = new File("C:\\Users\\795829\\Desktop\\1.txt"); // 字符流读取文件数据 BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); System.exit(-1); // TODO 测试用 // 字节流读取文件数据 FileInputStream fis = new FileInputStream(file); int n = 1024; byte buffer[] = new byte[n]; while ((fis.read(buffer, 0, n) != -1) && (n > 0)) { System.out.print(new String(buffer)); } fis.close(); } }

『叁』 java如何读取一个txt文件的所有内容

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class ReadFile { public static void main(String[] args) throws IOException { String fileContent = readFileContent(""); System.out.println(fileContent); } //参数string为你的文件名 private static String readFileContent(String fileName) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); while(content != null){ content = bf.readLine(); if(content == null){ break; } sb.append(content.trim()); } bf.close(); return sb.toString(); }}求采纳为满意回答。

『肆』 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文本中如据形如:123456789读入二维数组效果为: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文件的内容

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文件

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文件

如果对读写性能要求不是很高的话,读取TXT文件很简单。不考虑文本文件的编码的话,可以用一个FileReader读取磁盘上的文件即可如下:FileReader fr = new FileReader("文件路径");然后循环调用fr.read();将文本数据读入一个char数组。再解析数组中的字符,将网站名跟URL区分开,分别存入数据库即可。这是最简单的实现了。如果IO部分不熟悉,可以参考JDK文档中FileReader类、InputStreamReader类等java.io包中相关类的文档。 实际应用中,字符集编码是个很重要的问题。

『玖』 java读txt方法

Java读取txt文件和写入txt文件写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);}}


赞 (0)