Java文件操作大全(四)

26.移动一个文件夹下所有文件到另一个目录
//import java.io.*;
File movefile=new File(%%1);
File[] movefiles=movefile.listFiles();
for(int i=0;i<movefiles.length;i++){
if(movefiles[i].isFile()){
 int bytesum = 0;
 int byteread = 0;
 File oldfile = new File(movefiles[i]);
 try {
 if (oldfile.exists()) { //文件存在时
 InputStream inStream = new FileInputStream(oldfile); //读入原文件
 FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName()));
 byte[] buffer = new byte[5120];
 int length;
 while ( (byteread = inStream.read(buffer)) != -1) {
 bytesum += byteread; //字节数 文件大小
 //System.out.println(bytesum);
 fs.write(buffer, 0, byteread);
 }
 inStream.close();
oldfile.delete();
 }
 }
 catch (Exception e) {
 System.out.println("复制单个文件操作出错");
 e.printStackTrace();
 }
}


27.指定目录下搜索文件
//import java.io.*;
String filter="*.*";
String path=%%1;
 File file = new File(path);
 if(file.exists()) {
 if(file.isDirectory()) {
 File[] fileArray = file.listFiles();
 for(File f:fileArray) {
 if(f.isDirectory()) {
 doSearch(filter,f.getPath());
 } else {
 if(f.getName().indexOf(filter) >= 0) {
 countFiles++;
 result.append(f.getPath() + "/r/n");
 }
 }
 statusShow1.setText(f.getPath());
 }
 statusShow2.setText("The numbers of files had been found:" + countFiles);
 } else {
 System.out.println("Couldn't open the path!");
 }
 } else {
 System.out.println("The path had been apointed was not Exist!");
 }

28.打开对话框
//import java.io.*;
//import javax.swing.*;
JFileChooser Jfc = new JFileChooser(); //建立选择档案对话方块盒 Jfc
if (Jfc.isFileSelectionEnabled()) {
File %%1 = Jfc.getSelectedFile();
}

29.文件分割
//import java.io.*
try {
File f=new File(%%1);
FileInputStream fileInputStream = new FileInputStream(f);
byte[] buffer = new byte[fileInputStream.available()];
FileInputStream.read(buffer);
fileInputStream.close();
String strFileName = f.getName();
FileOutputStream fileOutputStream = new FileOutputStream(new File(%%2+"//"+ strFileName + "1"));
fileOutputStream.write(buffer,0,buffer.length/2);
fileOutputStream.close();
fileOutputStream = new FileOutputStream(new File(%%2+"//"+ strFileName + "2"));
fileOutputStream.write(buffer, buffer.length/2, buffer.length-buffer.length/2);
fileOutputStream.close();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.print("using FileStreamDemo src des");
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();


30.文件合并
//import java.io.*
String strFileName = %%1.substring(%%1.LastIndexOf("//") + 1);
try {
FileInputStream fileInputStream1 = new FileInputStream(new File(%%2 + strFileName + "1"));
FileInputStream fileInputStream2 = new FileInputStream(new File(%%2 + strFileName + "2"));
byte[] buffer = new byte[fileInputStream1.available()+fileInputStream2.available()];
FileInputStream.read(buffer, 0, fileInputStream1.available());
FileInputStream2.read(buffer, fileInputStream1.available(), fileInputStream2.available());
fileInputStream.close();
fileInputStream2.close();
FileOutputStream fileOutputStream = new FileOutputStream(new File(%%2+"//"+ strFileName));
fileOutputStream.write(buffer,0,buffer.length);
fileOutputStream.close();
catch(IOException e){
e.printStackTrace();
}
31.文件简单加密
//import java.io.*
try {
File f=new File((new File(%%1)).getPath()+"//enc_"+(new File(%%1)).getName().split("//")[1]);
String strFileName = f.getName();
FileInputStream fileInputStream = new FileInputStream(%%2+"//"+strFilename);
byte[] buffer = new byte[fileInputStream.available()];
FileInputStream.read(buffer);
fileInputStream.close();
for(int i=0;i<buffer.length;i++)
{
int ibt=buffer[i];
ibt+=100;
ibt%=256;
buffer[i]=(byte)ibt;
}
FileOutputStream fileOutputStream = new FileOutputStream(f);
fileOutputStream.write(buffer,0,buffer.length);
fileOutputStream.close();
}
catch(ArrayIndexOutOfBoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();


32.文件简单解密
//import java.io.*
try {
File f=new File(%%1);
String strFileName = f.getName();
FileInputStream fileInputStream = new FileInputStream(%%2+"//enc_"+strFilename);
byte[] buffer = new byte[fileInputStream.available()];
FileInputStream.read(buffer);
fileInputStream.close();
for(int i=0;i<buffer.length;i++)
{
int ibt=buffer[i];
ibt-=100;
ibt+=256;
ibt%=256;
buffer[i]=(byte)ibt;
}
FileOutputStream fileOutputStream = new FileOutputStream(f);
fileOutputStream.write(buffer,0,buffer.length);
fileOutputStream.close();
}
catch(ArrayIndexOutOfBoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();


33.写入ini文件属性
//import java.io.*;
//import java.util.*;
//import java.util.regex.*; 
if (configMap == null) {
String WriteBuffer = "";
configMap = new HashMap<String, Serializable>();
String strLine = null;
String currentNode = null;
String previousNode = null;
Vector<Properties> vec = new Vector<Properties>();
int row = 0;
FileReader fileReader = null;
try {
fileReader = new FileReader("Config.ini");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
while ((strLine = bufferedReader.readLine()) != null) {
String oneLine = strLine.trim();
if (oneLine.length() >= 1) {
Pattern p = Pattern.compile("//[//s*.*//s*//]");
int nodelen = oneLine.split("[;]").length;

String[] strArray1 = new String[4];
if (nodelen == 1) {
oneLine = oneLine.split("[;]")[0].trim();
} else if (nodelen == 2) {
strArray1[3] = oneLine.split("[;]")[1].trim();
oneLine = oneLine.split("[;]")[0].trim();
}
Matcher m = p.matcher(oneLine);
if (m.matches()) {
strArray1[0] = "@Node";
strArray1[1] = oneLine;
strArray1[2] = "";
} else {
int keylen = oneLine.split("=").length;
if (keylen == 1) {
strArray1[0] = "@Key";
strArray1[1] = oneLine.split("=")[0];
strArray1[2] = "";
} else if (keylen == 2) {
strArray1[0] = "@Key";
strArray1[1] = oneLine.split("=")[0];
strArray1[2] = oneLine.split("=")[1];
} else {
strArray1[0] = "@ElementError";
strArray1[1] = "";
strArray1[2] = "";
strArray1[3] = "";
}
}
if (strArray1[0].equals("@Node")) {
previousNode = currentNode;
currentNode = strArray1[1];
if (row > 0) {
configMap.put(previousNode, vec.elementAt(0));
vec.clear();
row = 0;
}
WriteBuffer += (oneLine + "/r/n");
} else if (strArray1[0].equals("@Key") && row == 0) {
Properties ht = new Properties();
ht.setProperty(strArray1[1], strArray1[2]);
if (strArray1[1].equals(%%1)) {
WriteBuffer += (%%1+"=" + %%2 + "/r/n");
} else
WriteBuffer += (oneLine + "/r/n");
vec.add(0, ht);
row++;
} else if (strArray1[0].equals("@Key") && row > 0) {
Properties ht2 = new Properties();
ht2.put(strArray1[1], strArray1[2]);
vec.clear();
vec.add(0, ht2);
WriteBuffer += (oneLine + "/r/n");
row++;
}
}
}
configMap.put(currentNode, vec.elementAt(0));
} catch (FileNotFoundException e) {
configMap = null;
e.printStackTrace();
} catch (IOException e) {
configMap = null;
e.printStackTrace();
} finally {
vec.clear();
try {
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter("Config.ini");
fw.write(WriteBuffer);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值