基本类型的读写
import java.io.*;
public class TestDataStream {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("MyFiles\\Data.txt");
DataOutputStream dos = new DataOutputStream(os);
dos.writeDouble(3.5);
dos.close();
//---------------------------------------------------------
InputStream is = new FileInputStream("MyFiles\\Data.txt");
DataInputStream dis = new DataInputStream(is);
double num = dis.readDouble();
dis.close();
System.out.println(num);
}
}
IO异常捕获
import java.io.*;
public class TestTryCatchFinally {
public static void main(String[] args) {
FileOutputStream os = null;
try {
os = new FileOutputStream("a.txt");
os.write('A');
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// ---------------------------------------------
InputStream is = null;
try {
is = new FileInputStream("a.txt");
byte[] cache = new byte[32];
for (;;) {
int n = is.read(cache);
if (n == -1) {
break;
}
for (int i = 0; i < n; i++) {
System.out.print((char) cache[i] + "\t");
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符的桥接
import java.io*;
public class TestBridgeStream {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("MyFiles\\Buffered.txt");
//将字节流转换成字符流
OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
PrintWriter pw = new PrintWriter(osw);
pw.println("人生就像一场戏");
pw.println("因为有缘才相聚");
pw.println("相扶到老不容易");
pw.println("是否更该去珍惜");
pw.close();
//描述:
//流是资源(重量级资源)
// InputStream is = TestBridgeStream.class.getResourceAsStream("/Buffered.txt");//获取读取TestBridgeStream.class文件时的流对象
InputStream is = new FileInputStream("MyFiles\\Buffered.txt");
//将字节流转换成字符流
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
for(;;){
String s = br.readLine();
if(s == null)
break;
System.out.println(s);
}
br.close();
}
}
输入一个文件名,查找并在当前目录复制
import java.io.*;
import java.util.Scanner;
public class TestFileCopy {
public static void main(String[] args) throws IOException {
File dir = new File("E:\\Baizhi\\projects\\Question10");
Scanner input = new Scanner(System.in);
System.out.print("请输入文件全名:");
String name = input.next();
System.out.println("输入为:"+name);
getFile(dir, name);
System.out.println("程序结束");
}
private static void getFile(File dir, String name) throws IOException {
File[] file = dir.listFiles();
if (file == null) {
System.out.println("文件路径为空!");
return;
}
for (File f : file) {
if(f.isFile()){
if(f.getName().equals(name)) {
copyFile(f);
}
}
else getFile(f,name);
}
}
private static void copyFile(File f) throws IOException {
InputStream is = new FileInputStream(f);
OutputStream os = new FileOutputStream("copy_" + f.getName());
byte[] cache = new byte[64];
for (; true;) {
int n = is.read(cache);
if (n == -1) {
break;
}
// 写入到另一个新文件中
os.write(cache, 0, n);
}
System.out.println("复制完成");
}
}
本文介绍了Java中基本类型的读写操作,包括使用DataOutputStream和DataInputStream进行读写;演示了如何通过try-catch-finally结构来捕获并处理IO异常;展示了字符流的桥接方法,即将字节流转换为字符流的过程;最后给出了一个实用的例子,即根据用户输入的文件名,在指定目录下查找文件并进行复制。
705

被折叠的 条评论
为什么被折叠?



