Java读写文件,
分享于 点击 13337 次 点评:155
Java读写文件,
Java读写文件最常用的就是InputStream和OutputStream,今天写了两个例子,作为记录,高手略过!
1.InputStream和OutputStream
package com.wicresoft.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class CopyFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream in = null;
FileOutputStream out = null;
System.out.println("copy file start");
try {
//in = new FileInputStream("input.txt");
//out = new FileOutputStream("output.txt");
in = new FileInputStream("D:/MyEclipseProject/IODemo/src/com/wicresoft/demo/input.txt");
out = new FileOutputStream("D:/MyEclipseProject/IODemo/src/com/wicresoft/demo/output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("copy file success");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.sys.in
package com.wicresoft.demo;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadConsole {
public static void main(String args[]) throws IOException
{
InputStreamReader cin = null;
try {
cin = new InputStreamReader(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
do {
c = (char) cin.read();
System.out.print(c);
} while(c != 'q');
}finally {
if (cin != null) {
cin.close();
}
}
}
}
关于Java i/o 更多详细内容:http://www.tutorialspoint.com/java/java_files_io.htm
相关文章
- 暂无相关文章
用户点评