java 拷贝文件和读取ini文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/earbao/article/details/70238698
package com.zryf.sotp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class CopyAndMove {
	
	public static void copy(File src, File dst) throws IOException {
		FileInputStream inStream = null;
		try {
			inStream = new FileInputStream(src);
			FileOutputStream outStream = null;
			try {
				outStream = new FileOutputStream(dst);
				FileChannel inChannel = inStream.getChannel();
				FileChannel outChannel = outStream.getChannel();
				inChannel.transferTo(0, inChannel.size(), outChannel);
			} finally {
				if (outStream != null)
				{
					outStream.flush();
					outStream.close();
				}
			}
		} finally {
			if (inStream != null)
				inStream.close();
		}
	}

	public static boolean move(File src, File dst) {
		try {
			copy(src, dst);
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return src.delete();
	}
	public static String readFromFileByText(File file) throws IOException 
	{
		String line;
		StringBuffer sb = new StringBuffer();
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		br.close();
		fr.close();
		return sb.toString();
	}

	public static boolean writeToFileByText(File file,String text) throws IOException 
	{
		FileWriter fw = new FileWriter(file);
		fw.write(text);
		fw.flush();
		fw.close();
		return true;
	}

	public static void main(String[] args) throws IOException {
		
		copy(new File("D:\\pojie\\packages.list"), new File("D:\\pojie\\test\\out1.txt"));
		move(new File("D:\\pojie\\test\\out1.txt"), new File("D:\\pojie\\test\\out2.txt"));
		System.out.println("ok");
	}

}
package com.zryf.sotp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IniFile {

	private Map<String, String> mIni = new HashMap<String, String>();

	public IniFile(File file) throws IOException {
		String line;
		Pattern pattern = Pattern.compile("\\s*([^=]*)=(.*)");
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		while ((line = br.readLine()) != null)
			if (!line.startsWith("#")) {
				Matcher matcher = pattern.matcher(line);
				if (matcher.matches()) {
					String key = matcher.group(1).trim();
					String value = matcher.group(2).trim();
					mIni.put(key, value);
				}
			}
		br.close();
		fr.close();
	}

	public String get(String key, String defaultvalue) {
		return mIni.get(key);
	}
	public static void main(String[] args) throws IOException {
		IniFile iniFile=new IniFile(new File("c://test.ini"));
		String data=iniFile.get("ID", "1234");
		System.out.println(data);
	}
}


展开阅读全文

没有更多推荐了,返回首页