获取文件内容java|java中如何从文件中读取数据

获取文件内容java|java中如何从文件中读取数据的第1张示图

㈠ 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文件的内容

能有的,很简单,readLine即可,然后封装到Map里面,key就是序号,value就是后面的值

㈢ java中如何从文件中读取数据

分为读字节,读字符两种读法◎◎◎FileInputStream 字节输入流读文件◎◎◎public class Maintest {public static void main(String[] args) throws IOException {File f=new File("G:\\just for fun\\xiangwei.txt");FileInputStream fin=new FileInputStream(f);byte[] bs=new byte[1024];int count=0;while((count=fin.read(bs))>0){String str=new String(bs,0,count);//反复定义新变量:每一次都 重新定义新变量,接收新读取的数据System.out.println(str);//反复输出新变量:每一次都 输出重新定义的新变量}fin.close();}}◎◎◎FileReader 字符输入流读文件◎◎◎public class Maintest {public static void main(String[] args) throws IOException {File f=new File("H:\\just for fun\\xiangwei.txt");FileReader fre=new FileReader(f);BufferedReader bre=new BufferedReader(fre);String str="";while((str=bre.readLine())!=null)//●判断最后一行不存在,为空{System.out.println(str);}bre.close(); fre.close();}}

㈣ java文件读取指定内容

给你写了一个小方法,应该满足你的要求了://url是你要读取的文件的路径,是所要求的包含的字符串如这里是“COMMON.9006 – 000332”。public static void readWantedText(String url, String wanted) { try { FileReader fr = new FileReader(url); BufferedReader br = new BufferedReader(fr); String temp = "";// 用于临时保存每次读取的内容 while (temp != null) { temp = br.readLine(); if (temp != null && temp.contains(wanted)) { System.out.println(temp); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }用的话直接调用这个方法就可以了:例如readWantedText("D:\\test.txt", "COMMON.9006 – 000332");//注意java路径需要在每条\前面在加条\表示转义。

㈤ 关于JAVA中读取文件内容的问题

我就做到这个程度了。细节自己在把握一下把。写得很随意。所以不太好读。需要一点正则的知识。import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.math.BigInteger;import java.util.ArrayList;import java.util.Scanner;import java.util.Stack;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main { public static void main(String[] args) throws IOException { File file = new File("in.txt");//读入文件,放在项目文件夹下 FileReader in = new FileReader(file); char[] c = new char[1024]; in.read(c); String input = String.valueOf(c); String s0 = "\\[.*\\]\\s*=\\s*\\{[^\\{\\}]*},"; String s1 = "\\[.*\\]\\s*=.*,"; String s2 = "\\[\".*\"\\]"; Pattern p0 = Pattern.compile(s0); Pattern p1 = Pattern.compile(s1); Pattern p2 = Pattern.compile(s2); Matcher m0 = p0.matcher(input); int m = 1; while (m0.find()) { Matcher m2 = p2.matcher(m0.group()); if(m2.find()) System.out.println("玩家" + m++ + " : " + m2.group()); Matcher m1 = p1.matcher(m0.group()); int n = 1; while (m1.find()) { System.out.println("属性" + n++ + " : " + m1.group()); } } }}

㈥ java怎么读取txt文件内容

版publicstaticvoidmain(String[]args){权try{RandomAccessFileraf=newRandomAccessFile("x:/xxx.xxx","rw");Stringstr="";while(raf.readLine()!=null){str+=raf.readLine();}System.out.println(str);}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}}

㈦ 如何用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 报的错,用相对路径会存在问题,因为一般eclipse会将class文件编译到bin里,程序就会认为 用户.txt 在bin\*\xxxx.class的目录中,所以没有找到。建议放在项目根目录,用类加载器加载文件,或直接绝对路径。

㈨ JAVA读取文件内容的问题

import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class test { public static void main(String[] args) { File file = new File("D:/1.txt"); try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String Ls; System.out.println("读取中,文件的内容如下:"); while(( Ls = br.readLine())!=null){ System.out.println(Ls); //Ls = br.readLine(); 此句去掉 } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}楼主要去掉while里面的Ls = br.readLine(); 这句,每次调用readline后指针会指向下一行

㈩ java读取指定目录下的文件内容

publicclassReadFromFile{/***以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/(StringfileName){Filefile=newFile(fileName);InputStreamin=null;try{System.out.println("以字节为单位读取文件内容,一次读一个字节:");//一次读一个字节in=newFileInputStream(file);inttempbyte;while((tempbyte=in.read())!=-1){System.out.write(tempbyte);}in.close();}catch(IOExceptione){e.printStackTrace();return;}try{System.out.println("以字节为单位读取文件内容,一次读多个字节:");//一次读多个字节byte[]tempbytes=newbyte[100];intbyteread=0;in=newFileInputStream(fileName);ReadFromFile.showAvailableBytes(in);//读入多个字节到字节数组中,byteread为一次读入的字节数while((byteread=in.read(tempbytes))!=-1){System.out.write(tempbytes,0,byteread);}}catch(Exceptione1){e1.printStackTrace();}finally{if(in!=null){try{in.close();}catch(IOExceptione1){}}}}/***以字符为单位读取文件,常用于读文本,数字等类型的文件*/(StringfileName){Filefile=newFile(fileName);Readerreader=null;try{System.out.println("以字符为单位读取文件内容,一次读一个字节:");//一次读一个字符reader=newInputStreamReader(newFileInputStream(file));inttempchar;while((tempchar=reader.read())!=-1){//对于windows下,这两个字符在一起时,表示一个换行。//但如果这两个字符分开显示时,会换两次行。//因此,屏蔽掉,或者屏蔽。否则,将会多出很多空行。if(((char)tempchar)!=''){System.out.print((char)tempchar);}}reader.close();}catch(Exceptione){e.printStackTrace();}try{System.out.println("以字符为单位读取文件内容,一次读多个字节:");//一次读多个字符char[]tempchars=newchar[30];intcharread=0;reader=newInputStreamReader(newFileInputStream(fileName));//读入多个字符到字符数组中,charread为一次读取字符数while((charread=reader.read(tempchars))!=-1){//同样屏蔽掉不显示if((charread==tempchars.length)&&(tempchars[tempchars.length-1]!='')){System.out.print(tempchars);}else{for(inti=0;i<charread;i++){if(tempchars[i]==''){continue;}else{System.out.print(tempchars[i]);}}}}}catch(Exceptione1){e1.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOExceptione1){}}}}/***以行为单位读取文件,常用于读面向行的格式化文件*/(StringfileName){Filefile=newFile(fileName);BufferedReaderreader=null;try{System.out.println("以行为单位读取文件内容,一次读一整行:");reader=newBufferedReader(newFileReader(file));StringtempString=null;intline=1;//一次读入一行,直到读入null为文件结束while((tempString=reader.readLine())!=null){//显示行号System.out.println("line"+line+":"+tempString);line++;}reader.close();}catch(IOExceptione){e.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOExceptione1){}}}}/***随机读取文件内容*/(StringfileName){RandomAccessFilerandomFile=null;try{System.out.println("随机读取一段文件内容:");//打开一个随机访问文件流,按只读方式randomFile=newRandomAccessFile(fileName,"r");//文件长度,字节数longfileLength=randomFile.length();//读文件的起始位置intbeginIndex=(fileLength>4)?4:0;//将读文件的开始位置移到beginIndex位置。randomFile.seek(beginIndex);byte[]bytes=newbyte[10];intbyteread=0;//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。//将一次读取的字节数赋给bytereadwhile((byteread=randomFile.read(bytes))!=-1){System.out.write(bytes,0,byteread);}}catch(IOExceptione){e.printStackTrace();}finally{if(randomFile!=null){try{randomFile.close();}catch(IOExceptione1){}}}}/***显示输入流中还剩的字节数*/(InputStreamin){try{System.out.println("当前字节输入流中的字节数为:"+in.available());}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){StringfileName="C:/temp/newTemp.txt";ReadFromFile.readFileByBytes(fileName);ReadFromFile.readFileByChars(fileName);ReadFromFile.readFileByLines(fileName);ReadFromFile.readFileByRandomAccess(fileName);}}

未经允许不得转载:山九号 » 获取文件内容java|java中如何从文件中读取数据

赞 (0)