package shi; import java.io.*; public class wenjianIO {
public static void main(String agrs[]){
FileInputStream fis=null;
File f=new File("F:/test/javakc.txt"); try {
fis=new FileInputStream(f);
byte[]arr=new byte[(int)f.length()];
fis.read(arr);
String str= new String (arr);
System.out.println(str); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fis!=null){
try{
fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}
} } }
java向文件中读写内容
package shi; import java.io.*; public class wenjianIO {
public static void main(String agrs[]){
FileInputStream fis=null;
FileOutputStream fos=null;
File f=new File("F:/test/javakc.txt"); try {
// 向文件中写内容
fos=new FileOutputStream(f);
String s="你的小EZ,有走歪了";
byte[]b=s.getBytes();
fos.write(b); // 向文件中读取内容
fis=new FileInputStream(f);
byte[]b2=new byte[(int) f.length()];
fis.read(b2);
String str=new String (b2);
System.out.println(str); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fis!=null){
try{
fos.close();
fis.close(); }
catch(IOException e){
e.printStackTrace();
}
}
} }
}