/**
* 生成Crc
* @param
*/
public static String Crc32(String filePath){
CRC32 crc32 = new CRC32();
crc32.update(getBytes(filePath));
long value = crc32.getValue();
return Long.toHexString(value)+"";
}
/**
* 获得指定文件的byte数组
*/
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 删除已存储的文件
*/
public static void deletefile(String filePath) {
try {
// 找到文件所在的路径并删除该文件
File file = new File(filePath);
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
一行一行写入字符串到文件的操作
public static void writeDataToFile1(String content) {
try {
File file = new File(Contants.FILE_PATH_DIR + "testFH");
if (!file.exists()) {
file.createNewFile();
}
/**
* 为了提高写入的效率,使用了字符流的缓冲区。
* 创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联。
*/
//第二个参数意义是说是否以append方式添加内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
//使用缓冲区中的方法将数据写入到缓冲区中。
bw.newLine();
bw.write(content);
//使用缓冲区中的方法,将数据刷新到目的地文件中去。
bw.flush();
//关闭缓冲区,同时关闭了FileWriter流对象
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 一行一行写入文件,适合字符写入,若写入中文字符时会出现乱码
* <p>
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*/
public static void writeDataToFile2(String content) {
try {
File file = new File(Contants.FILE_PATH_DIR + "testFH");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file,true);
//写入中文字符时会出现乱码
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content + "\t\n");
//使用缓冲区中的方法,将数据刷新到目的地文件中去。
bw.flush();
//关闭缓冲区,同时关闭了FileWriter流对象
bw.close();
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 一行一行写入文件,适合字符写入,若写入中文字符时会出现乱码
* <p>
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*/
public static void writeDataToFile3(String content) {
try {
File file = new File(Contants.FILE_PATH_DIR + "testFH");
if (!file.exists()) {
file.createNewFile();
}
//写入中文字符时解决中文乱码问题
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(content + "\t\n");
//注意关闭的先后顺序,先打开的后关闭,后打开的先关闭
bw.close();
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
一行一行读文件操作
/**
* 一行一行读取文件,适合字符读取,若读取中文字符时会出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void readFile01() throws IOException {
FileReader fr=new FileReader("E:/phsftp/evdokey/evdokey_201103221556.txt");
BufferedReader br=new BufferedReader(fr);
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
fr.close();
}
/**
* 一行一行读取文件,解决读取中文字符时出现乱码
*
* 流的关闭顺序:先打开的后关,后打开的先关,
* 否则有可能出现java.io.IOException: Stream closed异常
*
* @throws IOException
*/
@Test
public void readFile02() throws IOException {
FileInputStream fis=new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt");
InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
//简写如下
//BufferedReader br = new BufferedReader(new InputStreamReader(
// new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt"), "UTF-8"));
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
isr.close();
fis.close();
}
//一行行写 public static void writeDataToFile() { try { Charset charset = Charset.forName("US-ASCII"); String path = "D:/test/test.txt"; String s = "Hello moto"; Path p = Paths.get(path); BufferedWriter writer = Files.newBufferedWriter(p, charset, StandardOpenOption.APPEND,StandardOpenOption.CREATE); writer.write(s + "\t\n"); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } }