`
ybhuxiao
  • 浏览: 189837 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

TestIO

    博客分类:
  • java
阅读更多
package com.djwl.test.studying;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import com.djwl.core.MisException;

/**
 * Description: <br>
 * 2010-3-31
 * 
 * @author huxiao kskr@qq.com
 */
public class TestIO {
	/**
	 * description: 返回windows当前桌面的路径<br>
	 * 
	 * @return Mar 22, 2010
	 * @author huxiao kskr@qq.com
	 */
	public static String getDeskPath() {
		return FileSystemView.getFileSystemView().getHomeDirectory().toString() + File.separator;
	}

	public static String getWebRootPath() {
		return ClassLoader.getSystemClassLoader().getResource("").toString().replace("file:/", "").replace("/WEB-INF/classes/", "");
	}

	/**
	 * description: 把传过来的字符串生成为text文件<br>
	 * 
	 * @param str
	 *            Mar 22, 2010
	 * @author huxiao kskr@qq.com
	 */
	public static void createTextFileByStream(String str) {
		try {
			FileOutputStream fos = new FileOutputStream(new File(getDeskPath() + "test.createTextFileByStream.txt"));
			DataOutputStream dos = new DataOutputStream(fos);
			dos.writeBytes(str);
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		} catch (IOException e) {
			// TODO: handle exception
		}
	}

	/**
	 * description: 把传过来的字符串生成为text文件<br>
	 * 
	 * @param str
	 *            Mar 22, 2010
	 * @author huxiao kskr@qq.com
	 */
	public static void createTextFileByWriter(String str) {
		try {
			Writer writer = new FileWriter(new File(getDeskPath() + "test.createTextFileByWriter.txt"));
			writer.write(str);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}

	/**
	 * description: 获取当前类路径<br>
	 * 
	 * @return Mar 22, 2010
	 * @author huxiao kskr@qq.com
	 */
	public static String getBasePath() {
		String path = ClassLoader.getSystemResource("").toString().replace("file:/", "").replace("/WEB-INF/classes/", "");
		;
		return path;
	}

	/**
	 * description: 按行读取文件<br>
	 * Mar 22, 2010
	 * 
	 * @author huxiao kskr@qq.com
	 */
	public static void readFileByLine() {
		List<String> list = new ArrayList<String>();
		try {
			String filepath = getWebRootPath() + "/test/1.txt";
			FileReader reader = new FileReader(filepath);
			BufferedReader br = new BufferedReader(reader);
			String s1 = null;
			while ((s1 = br.readLine()) != null) {
				list.add(s1);
			}
			br.close();
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(list.size() + "" + list);
	}

	/**
	 * description: 输入流读取文件<br>
	 * Mar 22, 2010
	 * 
	 * @author huxiao kskr@qq.com
	 */
	public static void readFileByInputStream() {
		String filepath = getWebRootPath() + "/test/test.txt";
		FileInputStream fis = null;
		File file = new File(filepath);
		try {
			fis = new FileInputStream(file);
			byte[] b = new byte[(int) file.length()];
			while (fis.read(b) != -1) {
			}
			System.out.println(new String(b));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 功能描述:创建path目录及其父目录<BR>
	 * 
	 * @param path
	 * @author 胡晓<BR>
	 *         时间:2009-10-22 下午05:01:33<BR>
	 */
	public static void createFolder(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	/**
	 * 功能描述:把filename从from文件夹移动到to文件夹<BR>
	 * 
	 * @param from
	 * @param to
	 * @author 胡晓<BR>
	 *         时间:2009-10-22 下午05:08:21<BR>
	 */
	public static void moveFile(String from, String to, String filename) {
		createFolder(to);
		File file = new File(from, filename);
		if (file.isFile()) {
			System.out.println("移动" + filename);
			file.renameTo(new File(to, file.getName()));
		} else {
			System.out.println(filename + "不存在");
		}
	}

	/**
	 * description: 其他零碎的<br>
	 * 
	 * @param args
	 *            Mar 22, 2010
	 * @author huxiao kskr@qq.com
	 */
	public static void main(String[] args) {
		File file = new File("");

		// move file or rename file
		file.renameTo(new File("this is a new file with a new fileName"));

		// 判断文件或目录是否存在,文件和目录都是true
		if (file.exists()) {

		}

		// 判断文件是否存在,文件时true,目录是false
		if (file.isFile()) {

		}

		System.out.println(getWebRootPath());

		// 获取桌面路径
		System.out.println(FileSystemView.getFileSystemView().getHomeDirectory());
		// 获取我的文档路径
		System.out.println(FileSystemView.getFileSystemView().getDefaultDirectory());
		// 获取webroot绝对路径,可以在static method中使用
		System.out.println(ClassLoader.getSystemResource(""));
		// 获取webroot绝对路径,在非static method中可使用this
		System.out.println(new Backup().getClass().getClassLoader().getResource(""));
		// 获取类的绝对路径,在非static method中可使用this
		System.out.println(new Backup().getClass().getResource(""));
	}

	/**
	 * 功能描述:删除单个文件<BR>
	 * 
	 * @param file
	 * @author:杨凯<BR>
	 *            时间:Apr 16, 2009 1:55:11 PM<BR>
	 */
	public static void deleteFile(File file) {
		file.delete();
	}

	/**
	 * 功能描述:删除目录,并删除该目录下的所有文件<BR>
	 * 
	 * @param dir
	 * @return
	 * @author:杨凯<BR>
	 *            时间:Apr 16, 2009 1:55:21 PM<BR>
	 */
	public static boolean deleteDirectory(File dir) {
		if ((dir == null) || !dir.isDirectory()) {
			throw new MisException("要删除的目录不存在,或者不是目录");
		}

		File[] files = dir.listFiles();
		int sz = files.length;

		for (int i = 0; i < sz; i++) {
			if (files[i].isDirectory()) {
				if (!deleteDirectory(files[i])) {
					return false;
				}
			} else {
				if (!files[i].delete()) {
					return false;
				}
			}
		}

		if (!dir.delete()) {
			return false;
		}
		return true;
	}

}




★、节点流类型


★、处理流类型

  • 大小: 103.3 KB
  • 大小: 106.7 KB
分享到:
评论

相关推荐

    testIO项目

    仅供初学者参考,解压导入到eclipse就可运行

    HX8227A01_TESTIO.pdf

    HX8227A01_TESTIO.pdf

    TestIO.java

    TestIO.java

    IO操作.pdf

    一本清华大写java IO课程书。详细介绍了java IO操作的各个类,可以一睹高级学府的老师是如何讲述java IO的。

    RTL8019AS原厂开发资料

    RTL8019AS原厂开发资料 This package included ... "testio16.com": This is a program can check register status on DOS environment. For more information, please feel free contact nicfae@realtek.com.tw

    Java复习题及答案

    class TestIO { public static void main(String[] args) { try{ RandomAccessFile raf=new RandomAccessFile("test.dat","r"); int i=raf.readInt(); } catch(IOException e){System.out.println("IO ...

    jsr80 java 访问 usb

    dW ...IBM developerWorks® 技术主题 软件下载 社区 技术讲座 搜索 developerWorks 打印本页面用电子邮件发送本页面新浪微博人人网腾讯微博搜狐微博网易微博DiggFacebookTwitterDeliciousLinked In ...

    learning-unit-testing-with-node

    研究框架Mocha&Chai(这是一个用于单元测试的库(NodeJS)) 诗乃(模拟图书馆) 存根Gulp(任务执行器) 咕unt声(Taskrunner)学科研究测试业务逻辑(例如testBusinessLogic文件夹) 测试IO(例如testIO文件夹) ...

Global site tag (gtag.js) - Google Analytics