* 没有用文本就用字节
* 用到文本就要字符
一、字节的写入和读取
package File类;
import java.io.*;
public class 文件写入和读取 {
public static void main(String[] args) {
File file = new File("./1.js");
// writeFile(file, "欧富华");
// appendFileContent(file, "欧富华");
getFileContent(file);
}
// 写入文件就是输出流
public static void writeFile(File file, String content) {
// 写入就是输出流
try {
FileOutputStream out = new FileOutputStream(file);
out.write(content.getBytes());
System.out.println("写入成功");
out.close(); // 关闭流
} catch (Exception e) {
}
}
// 文件追加内容
public static void appendFileContent(File file, String content) {
// 写入就是输出流
try {
FileOutputStream out = new FileOutputStream(file, true);
out.write(content.getBytes());
System.out.println("追加写入成功");
out.close(); // 关闭流
} catch (Exception e) {
}
}
// 文件读取就是输入流
public static void getFileContent(File file) {
try {
int len = 0;
StringBuilder stringBuilder = new StringBuilder();
byte[] bytes = new byte[1024]; // 定义读取数量
FileInputStream fileInputStream = new FileInputStream(file);
// 不等于-1就是存在字节
while ((len=fileInputStream.read(bytes)) != -1) {
System.out.println((char)len);
System.out.println(11111);
stringBuilder.append(new String(bytes), 0, 7);
}
fileInputStream.close(); // 关闭流
System.out.println(stringBuilder);
fileInputStream.close();
} catch (Exception e) {
}
}
}
二、字符写入和读取
package File类;
import java.io.*;
public class 字符write和read {
public static void main(String[] args) {
File file = new File("1.js");
wirte(file);
read(file);
}
public static void wirte(File file) {
try {
Writer writer = new FileWriter(file, true);
writer.write("我叫欧富华");
writer.close();
} catch (Exception e) {
}
}
public static void read(File file) {
try {
Reader reader = new FileReader(file);
char[] c = new char[1]; // 一个字符
StringBuilder stringBuilder = new StringBuilder();
while ((reader.read(c)) != -1) {
stringBuilder.append(new String(c));
}
reader.close();
System.out.println(stringBuilder);
} catch (Exception e) {
}
}
}
三、案例(文件的复制)
package File类;
import java.io.*;
public class copyDemo {
public static void main(String[] args) {
copyBytes("1.js", "copy.js");
copyChars("1.js", "copy1.js");
System.out.println("复制成功");
}
// 字节的复制
static void copyBytes(String src, String targer) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File srcFile = new File(src);
File targerFile = new File(targer);
fileInputStream = new FileInputStream(srcFile); // 读取
fileOutputStream = new FileOutputStream(targerFile); // 写入
byte[] bytes = new byte[1024];
int len = -1;
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (fileInputStream != null) fileInputStream.close();
if (fileOutputStream != null) fileOutputStream.close();
} catch (Exception e) {
}
}
}
public static void copyChars(String src, String targer) {
Reader reader = null;
Writer writer = null;
try {
File srcFile = new File(src);
File targerFile = new File(targer);
reader = new FileReader(srcFile);
writer = new FileWriter(targerFile);
int len = -1;
char[] chars = new char[1];
while ((len = reader.read(chars)) != -1) {
writer.write(chars, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (writer != null) writer.close();
if (reader != null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
网友评论