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

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

A. 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();}}

B. 关于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()); } } }}

C. 初学Java,请教如何读取文件内容

try { String line = null; String path = "F:\\a.csv"; BufferedReader reader = new BufferedReader(new FileReader(path)); while ((line = reader.readLine()) != null) { String[] item = line.split(","); for (String a : item) { if (a.indexOf("\"") == 0) { String b = a.substring(1, a.length() – 1); System.out.print(b + ","); } else { System.out.print(","); } } System.out.println(); } reader.close(); } catch (Exception e) { e.printStackTrace(); }刚写的一个读取CSV的小东西CSV中格式:"1","2","3","4""2","3","4","5"读取出来打印在控制台上是1,2,3,4,2,3,4,5,

D. 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);}}

E. 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后指针会指向下一行

F. 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();}}

G. java读取文件的内容检索

你会写文件 就应该会读文件吧 每一行读出来进行判断就行了啊

H. java如何获取文件信息

File 类是对文件和文件夹的抽象,包含了对文件和文件夹的多种属性和操作方法。File类的常用方法如下表:返回方法说明String getName 获取文件名称 String getParent 获取文件的父路径字符串 String getPath 获取文件的相对路径字符串 String getAbsolutePath 获取文件的绝对路径字符串 boolean exists 判断文件或者文件夹是否存在 boolean isFile 判断是不是文件类型 boolean isDirectory 判断是不是文件夹类型 boolean delete 删除文件或文件夹,如果删除成功返回结果为true boolean mkdir 创建文件夹,创建成功返回true boolean setReadOnly 设置文件或文件夹的只读属性 long length 获取文件的长度 long lastModified 获取文件的最后修改时间 String[ ] list 获取文件夹中的文件和子文件夹的名称,并存放到字符串数组中

I. 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(); }}求采纳为满意回答。

J. 如何用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获取文件的内容|java中如何从文件中读取数据

赞 (0)