package trans;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ObjectFileTrans {
/*文件写入路径*/
public static final String LOACLFILEPATH_STRING = "D:\\test\\";
/*文件写入日期*/
static Date date = new Date();
static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH");
static String nowTimeHour = simpleDateFormat.format(date);
/*文件写入*/
public static void writeObject(String infoStr, String dealTime) {
try {
File inforFile = new File(ObjectFileTrans.LOACLFILEPATH_STRING + dealTime + ".txt");
FileWriter fileWriter = new FileWriter(inforFile, true);
fileWriter.write(infoStr);
fileWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*文件读取*/
public static String readObject() {
StringBuffer buffer = new StringBuffer();
try {
/*文件读取路径*/
FileReader fileReader = new FileReader("D:\\test\\02.txt");
/*文件读取缓冲区*/
char[] chs = new char[1024];
while(fileReader.read(chs) != -1) {
System.out.println(chs);
buffer.append(chs);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) {
ObjectFileTrans objectFileTrans = new ObjectFileTrans();
/*文件读取测试*/
String infoString = objectFileTrans.readObject();
/*文件写入测试*/
objectFileTrans.writeObject(infoString, nowTimeHour);
}
}