package com.happy.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RenName {
public static void main(String[] args) {
// System.out.println(RenName.readFile(Thread.currentThread()
// .getContextClassLoader().getResource("").toString(),
// RenName.class));
List<String> list = RenName
.getFileName("H:/学习/软件/Translate/Kingsoft/每日一句");
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next();
System.out.println(string);
if (readFile(string).length() > 10) {
RenName.renFileName(string, RenName.readFile(string).substring(
0, 10));
}
}
System.out.println("O V E R");
}
/**
* 读取: BufferedReader br = new BufferedReader(new InputStreamReader(new
* FileInputStream(File file), "UTF-8")); 写入: BufferedWriter bw = new
* BufferedWriter(new OutputStreamWriter(new FileOutputStream(File file),
* "UTF-8"));
*
* @param fileName
* @return
*/
public static String readFile(String fileName) {
String s1, s2 = "";
try {
// BufferedReader in = new BufferedReader(new FileReader(fileName));
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "GBK"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
while ((s1 = in.readLine()) != null) {
s2 += s1 + "/n";
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return s2;
}
/**
* @param fileName
* @return
*/
public static List<String> getFileName(String fileName) {
List<String> list = new ArrayList<String>();
File file = new File(fileName);
if (file.isDirectory() && file.exists()) {
String[] files = file.list();
for (String string : files) {
list.add(file.getPath() + "//" + string);
}
}
return list;
}
public static void renFileName(String fileName, String newFileName) {
File file = new File(fileName);
if (file.isFile() && file.exists()) {
// 第10天
// System.out.println(file.getName().substring(0,
// file.getName().lastIndexOf('.')));
// txt
// System.out.println(file.getName().substring(file.getName().lastIndexOf('.')+1));
// H:/学习/软件/Translate/Kingsoft/好好学习/第10天.txt
// System.out.println(file);
// H:/学习/软件/Translate/Kingsoft/好好学习
// System.out.println(file.getParent());
file
.renameTo(new File(file.getParent()
+ "/"
+ file.getName().substring(0,
file.getName().lastIndexOf('.'))
+ newFileName
+ file.getName().substring(
file.getName().lastIndexOf('.'))));
}
}
/**
* @return
*/
@SuppressWarnings("unchecked")
public static String readFile(String fileName, Class clz) {
String s1, s2 = "";
try {
BufferedReader in = new BufferedReader(new FileReader(
changeFileName(fileName, clz)));
try {
while ((s1 = in.readLine()) != null) {
s2 += s1 + "/n";
}
in.close();
// System.out.println(s2);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return s2;
}
/**
* 得到当前类的绝对地址
*
* @param fileName
* @param clz
* @return
*/
@SuppressWarnings("unchecked")
public static String changeFileName(String fileName, Class clz) {
String temp = fileName.substring(6);
fileName = clz.getCanonicalName();
fileName = fileName.replace(".", "/");
fileName = temp + fileName + ".class";
// E:/MyEclipse-8.6/Works/ReadFile/bin/com/happy/file/RenName.class
return fileName;
}
}