读写文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DXshuju {
public static void main(String[] args) {
String str ="chinayang 朋超阿贾克斯不断放开骄傲恨不得能看见俺"
+ "的合适的撒娇大家大空间的哈克电话卡的话啊啊啊啊啊啊啊啊"
+ "的合适的撒娇大家大空间的哈克电话卡的话啊啊啊啊啊啊啊啊\n"
+ "的合适的撒娇大家大空间的哈克电话卡的话啊啊啊啊啊啊啊啊";
//关连文件的具体路径
try {
FileOutputStream fs = new FileOutputStream("HELLO.java"); //创建实例对象
//写文件
try {
byte [] b=new byte [5000];
b=str.getBytes();
int off=0;
int len=b.length;
System.out.println(len);
fs.write(b, off, len);
System.out.println("fs ok");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fs.close(); //关闭输出流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream fs1 =new FileInputStream("HELLO.java"); //创建对象 关联文件
File f =new File("HELLO.java"); //获取文件属性
long an = f.length();
byte[] b = new byte[(int) an];
//读文件
try {
int rEnd = fs1.read(b); //整块的读
System.out.println( new String(b) );
for(int i=0;i<an;i++){
int xie = fs1.read();
if(-1!=xie){
System.out.print((char)xie);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fs1.close(); //关闭输出流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}