1.文件的创建
String mailBodypath = configPath +"/mail"+".txt";
String sendTopath = configPath +"/To"+".properties";
根据传入的不同,可以生成不同格式的文件。
private static void CreateTxt(String dir)
{try {
File dirPath = new File(dir);
if (!dirPath.exists()) {
dirPath.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.文件的复制
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
3.文件的写入
public static void contentToSendTo(String filePath,String inf1 , String inf2) {try{
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true));
writer.write(inf1); //写入第一行数据
writer.write("\r\n"); //换行
writer.write(inf2); //写入第二行数据
writer.write("\r\n"); //换行
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}
4.文档的读取
public static void readFile(String filePath){
try {
String encoding="utf-8";
File file=new File(filePath);
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null)
{
System.out.println("\""+ lineTxt + "\",");
}
read.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
5.文档的删除
public void deleteExcel(File file){
try{
if (file.isFile()) {
file.delete();
}
}
catch(NullPointerException n){
System.out.println("Sorry,No such file");
}
}
6.生成.ini文件
public void writeINI(String name) throws IOException {
StringBuilder sb = new StringBuilder("");
for (String section : iniFile.keySet()) {
sb.append("[").append(section).append("]").append("\n");
Map<String, Object> map = iniFile.get(section);
Set<String> keySet = map.keySet();
for (String key : keySet) {
sb.append(key).append("=").append(map.get(key)).append("\n");
}
}
File file = new File(name);
if (!file.exists()) {
file.createNewFile();
}
else{
return;
}
try {
OutputStream os = new FileOutputStream(file);
os.write(sb.toString().getBytes());
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}