📄 fileioclass.java
字号:
import java.io.*;
public class FileIOClass {
final static byte already_exists = 11;
final static byte create_failed = 12;
final static byte file_success = 13;
final static byte open_failed = 14;
File f = null;
FileOutputStream fout = null;
FileInputStream fin = null;
//创建一个新文件(将来要对这个文件进行写)
public byte createFile(String fname) {
f = new File(fname); //创建一个文件实例
if(f.exists()) { //如果文件存在
return already_exists;
}
try { //创建文件流
fout = new FileOutputStream(f);
}catch(IOException e) {
return create_failed;
}
return file_success; //文件创建成功
}
//打开一个文件(将来要对这个文件进行读)
public byte openFile(String fname) {
f = new File(fname); //创建一个文件实例
try { //创建文件流
fin = new FileInputStream(f);
}catch(IOException e) {
return open_failed;
}
return file_success; //文件打开成功
}
//返回文件的长
public long giveFileLength() {
return f.length();
}
//返回文件名
public String giveFilename() {
return f.getName();
}
//读文件,返回文件512个字节
public byte[] readFromFile() {
byte b[] = new byte[512];
try {
int n = fin.read(b,0,511);
if(n == -1) {
fin.close();
b[511] = 1 ; //最后一位是判断位
System.out.println("读文件结束.");
}
}catch(IOException e) {
System.out.println("读文件错误!");
b[511] = 1 ;
}
return b;
}
//写入文件,写文件512个字节
public void writeToFile(byte[] in) {
byte b[] = in ;
try {
fout.write(b,4,511);
}catch(IOException e) {
System.out.println("写入文件错误!");
}
}
//配合上面的函数,友好的关闭流
public void endWrite() {
try {
fout.close();
System.out.println("写入文件结束.");
}catch(IOException e) {
System.err.println(e.getStackTrace());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -