一.得到文件内容的两种方法
1.用io包中的FileInputStream(读取文件数据)类
package file;
import java.io.*;
import java.io.*;
public class Read {
public static void main(String args[]){
byte[] fbytes=new byte[100];
try{
FileInputStream file=new FileInputStream("D:\\io\\hello.txt");\\实例化对象
file.read(fbytes, 0, 100);\\从文件读到的是字节,返回的是整形数据
String str=new String(fbytes,0);\\将内容转化为字符串
System.out.println(str);
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
byte[] fbytes=new byte[100];
try{
FileInputStream file=new FileInputStream("D:\\io\\hello.txt");\\实例化对象
file.read(fbytes, 0, 100);\\从文件读到的是字节,返回的是整形数据
String str=new String(fbytes,0);\\将内容转化为字符串
System.out.println(str);
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
2.首先导入两个包: java.io.File 和 org.apach.commons.io.FileUtils .
然后:
File file=new File(类名.class.getResource("").getFile())//得到文件
String content=FileUtils.readFileToString(file);//得到文件内容
或者:
File file=new File(url);
扫描二维码关注公众号,回复:
16111863 查看本文章
File[] files=file.listFiles();