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;
}
}
★、节点流类型
[img]http://dl.iteye.com/upload/attachment/232573/f0372082-cea2-37b5-b862-57184298f429.png[/img]
★、处理流类型
[img]http://dl.iteye.com/upload/attachment/232575/156b0aeb-2da3-331e-b928-28f3147aaed8.png[/img]