import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
/**
* 通过流读取文件
*/
public class ReadFileDemo {
// 程序入口
public static void main(String[] args) {
String path = "c:/boot.ini";
File file = new File(path);
// 创建输入流
InputStream is;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println("文件 " + path + " 不存在。");
return;
}
// 开始读取
byte[] content = new byte[0]; // 保存读取出来的文件内容
byte[] buffer = new byte[10240]; // 定义缓存
try {
int eachTime = is.read(buffer); // 第一次读取。如果返回值为 -1 就表示没有内容可读了。
while (eachTime != -1) {
// 读取出来的内容放在 buffer 中,现在将其合并到 content。
content = concatByteArrays(content, buffer, eachTime);
eachTime = is.read(buffer); // 继续读取
}
} catch (IOException e) {
System.err.println("读取文件内容失败。");
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// 这里的异常可以忽略不处理
}
}
// 输出文件内容
String contentStr = new String(content);
System.out.println(contentStr);
}
/**
* 合并两个字节串
*
* @param bytes1 字节串1
* @param bytes2 字节串2
* @param sizeOfBytes2 需要从 bytes2 中取出的长度
*
* @return bytes1 和 bytes2 中的前 sizeOfBytes2 个字节合并后的结果
*/
private static byte[] concatByteArrays(byte[] bytes1, byte[] bytes2, int sizeOfBytes2) {
byte[] result = Arrays.copyOf(bytes1, (bytes1.length + sizeOfBytes2));
System.arraycopy(bytes2, 0, result, bytes1.length, sizeOfBytes2);
return result;
}
}
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* 将当前日期保存到文件
*/
public class SaveFileDemo {
public static void main(String[] args) throws IOException {
String path = "c:/now.txt";
File file = new File(path);
if (!file.exists() && !file.createNewFile()) {
System.err.println("无法创建文件。");
return;
}
OutputStream os = new FileOutputStream(file); // 创建输出流(前提是文件存在)
os.write(new Date().toString().getBytes()); // 将当前时间写入文件
os.close(); // 必须关闭流,内容才会写入文件。
System.out.println("文件写入完成。");
}
}
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* 读取文本文件
*/
public class ReadTextFileDemo {
// 程序入口
public static void main(String[] args) {
String path = "c:/boot.ini";
String content = "";
try {
Reader reader = new FileReader(path);
char[] buffer = new char[10240];
int count;
while ((count = reader.read(buffer)) != -1) {
content += new String(buffer, 0, count);
}
} catch (IOException e) {
System.err.println("读取文件失败。");
e.printStackTrace();
}
System.out.println(content);
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* 用 BufferedReader 读取文本文件
*/
public class ReadTextDemo2 {
public static void main(String[] args) {
String path = "c:/boot.ini";
String content = "";
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
while ((line = reader.readLine()) != null) {
content += line + "/n";
}
} catch (IOException e) {
System.err.println("读取文件失败。");
e.printStackTrace();
}
System.out.println(content);
}
}
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.EOFException;
/**
* ObjectOutputStream/ObjectInputStream 示例。
* 这两个类分别用于序列化和反序列化。
*/
public class SerializationDemo {
public static void main(String[] args) throws Exception {
String path = "c:/persons.data";
File f = new File(path);
if (!f.exists()) {
f.createNewFile();
}
writePersons(path);
readPersons(path);
}
// 从指定的文件中读取 Person 对象
private static void readPersons(String path) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
Person p;
while (true) {
try {
p = (Person) ois.readObject();
System.out.println(p);
} catch (EOFException e) {
break;
}
}
}
// 将 Person 对象保存到指定的文件中
private static void writePersons(String path) throws IOException {
Person[] persons = new Person[]{
new Person("张三", 23),
new Person("李四", 24)
};
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
for (Person person : persons) {
oos.writeObject(person);
}
oos.close();
}
///////////////////////////////////////////////////////////
private static class Person implements Serializable {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '/'' +
", age=" + age +
'}';
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有