📄 savefileio.java
字号:
package xn.tetris;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*存储最高分 最高等级 最高分创造者到文件myTetris.dat
* */
public class SaveFileIO {
private Tea tea;
private final static int[] KEY = new int[]{//加密解密所用的KEY
0x789f5645, 0xf68bd5a4,
0x81963ffa, 0x458fac58
};
private BufferedInputStream input;
private BufferedOutputStream output;
private FileInputStream fileInput;
private FileOutputStream fileOutput;
private static File saveFile = new File("myTetris.dat");
private String finalStr;
public SaveFileIO(){
tea = new Tea();
}
//从文件读出数据
public String[] readFromFile() throws IOException{
if(!saveFile.exists()){//若文件不存在
return new String[]{
"Are you the winner?", "0", "1"
};
}
fileInput = new FileInputStream(saveFile);
input = new BufferedInputStream(fileInput);
int length = input.read();
byte[] fileRead = new byte[length];
input.read(fileRead, 0, length);
String finalRead = decryptByTea(fileRead);
input.close();
return finalRead.split("/ /");
}
//往文件中写数据
public void writeToFile(String creatorName, int highestScore, int highestLevel) throws IOException{
fileOutput = new FileOutputStream(saveFile);
output = new BufferedOutputStream(fileOutput);
finalStr = creatorName + "/ /" + highestScore + "/ /" + highestLevel;
byte[] tempWrite = encryptByTea(finalStr);
int length = tempWrite.length;
byte[] finalWrite = new byte[length + 1];
finalWrite[0] = (byte) length;
System.arraycopy(tempWrite, 0, finalWrite, 1, length);
output.write(finalWrite, 0, finalWrite.length);
output.close();
}
//通过TEA算法加密需要存储到文件里的信息
private byte[] encryptByTea(String info){
byte[] temp = info.getBytes();
int n = 8 - temp.length % 8;//若temp的个数不足8的倍数,需要填充的位数
byte[] encryptStr = new byte[temp.length + n];
encryptStr[0] = (byte)n;
System.arraycopy(temp, 0, encryptStr, n, temp.length);
byte[] result = new byte[encryptStr.length];
for(int offset = 0; offset < result.length; offset += 8){
byte[] tempEncrpt = tea.encrypt(encryptStr, offset, KEY, 32);
System.arraycopy(tempEncrpt, 0, result, offset, 8);
}
return result;
}
//通过TEA算法解密存储在文件里的信息
private String decryptByTea(byte[] secretInfo){
byte[] decryptStr = null;
byte[] tempDecrypt = new byte[secretInfo.length];
for(int offset = 0; offset < secretInfo.length; offset += 8){
decryptStr = tea.decrypt(secretInfo, offset, KEY, 32);
System.arraycopy(decryptStr, 0, tempDecrypt, offset, 8);
}
int n = tempDecrypt[0];
return new String(tempDecrypt, n, decryptStr.length - n);
}
/*public static void main(String[] args){
String info = "www.blogjava.net/orangehf";
System.out.println("原数据:" + info);
SaveFileIO io = new SaveFileIO();
byte[] encryptInfo = io.encryptByTea(info);
System.out.print("加密后的数据:");
for(byte i : encryptInfo)
System.out.print(i + " ");
System.out.println();
String decryptInfo = io.decryptByTea(encryptInfo);
System.out.print("解密后的数据:");
System.out.println(decryptInfo);
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -