首页 > 其他 > 详细

文件读写&文件夹遍历

时间:2015-04-25 22:51:55      阅读:328      评论:0      收藏:0      [点我收藏+]


文件读写

 

读文件()

	private void readFile(File file) throws IOException {
		FileInputStream stream = null;
		stream = new FileInputStream(file);

		DataInputStream sysin = new DataInputStream(stream);
		String line = null;

		while ((line = sysin.readLine()) != null) {
			if (line.trim().equals("")) {
				continue;
			}
			System.out.println(line);
		}
	}


写文件

public void write(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists())
			file.createNewFile();
		FileOutputStream out = new FileOutputStream(file, false);
		String data = "aa\nbb";
		out.write(data.toString().getBytes("utf-8"));
		ParsePclntResult obj = new ParsePclntResult();
	}

递归删除指定文件夹下的文件(遍历文件)

private void cleanupRecursive(File file) {
		try {
			if (file.isDirectory()) {

				for (File child : file.listFiles()) {
					cleanupRecursive(child);
				}
				if (file.listFiles().length == 0) {
					file.delete();
				}
			} else {

				if (file.getName().endsWith(".xls")) {
					file.delete();
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage() + "\nFile:" + file.getAbsolutePath());
		}

	}






文件读写&文件夹遍历

原文:http://blog.csdn.net/tiankefeng19850520/article/details/45274139

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!