您的位置:首页 > 编程语言 > Java开发

Java IO

2016-07-18 15:01 411 查看
public class InputStreamTest2 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("d:\\data.txt");
InputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int off = 0;

int len = b.length;
while((off=in.read(b, 0, b.length))!=-1){
System.out.println(new String(b,0,off));
}
in.close();
}

}


拷贝指定的文件:

public class copyFileTest {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File src = new File("d:\\data.txt");
File desc = new File("d:\\desc.txt");

new copyFileTest().copyFile(src, desc);
}
public void copyFile(File src,File desc) throws IOException{
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(desc);
byte b[] = new byte[1024];
int start=0;
while((start=in.read(b, 0, b.length))!=-1){
out.write(b, 0, start);
out.flush();
}
in.close();
out.close();
}
}


字符流:

public class ReaderWriterTest {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream in = new FileInputStream("d:\\reader.txt");
FileOutputStream out = new FileOutputStream("d:\\writer.txt");
InputStreamReader isr = new InputStreamReader(in);
OutputStreamWriter osw = new OutputStreamWriter(out);
char b[] = new char[1024];
int c;
while((c=isr.read(b, 0, b.length))!=-1){
String s = new String(b, 0, c);
System.out.print(s);
osw.write(b, 0, c);
osw.flush();
}
isr.close();
osw.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: