一、FileInputStream 文件输入流
File类在java中表示抽象路径名,可以表示一个具体的文件也可以表示一个目录。
*/
public class FileDemo {
public static void main(String[] args) {
// File file = new File("a.txt");
//创建文件
// try {
// file.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// System.out.println(file.canExecute());
// System.out.println(file.canRead());
// System.out.println(file.canWrite());
//删除文件或者目录
// System.out.println(file.delete());
// System.out.println(file.exists());
//获取文件的绝对路径名
//获取文件绝对路径名的规范格式
// System.out.println(file.getCanonicalPath());
// } catch (IOException e) {
// e.printStackTrace();
// }
//获取文件或目录的名称
// System.out.println(file.getName());
//返回文件的父目录,如果文件路径名是一个具体的文件,此时返回null
// System.out.println(file.getParent());
// System.out.println(file.getPath());
// System.out.println(file.isFile());
// System.out.println(file.isDirectory());
// System.out.println(file.length());
// File file2 = new File("123.txt");
//即使没有这个文件,也可以获取文件的绝对路径名
// System.out.println(file2.getAbsolutePath());
// System.out.println(file.lastModified());
// System.out.println("***************************");
// File file3 = new File("D:\\idea_workspace");
// String[] strings = file3.list();
// for(String path : strings){
// System.out.println(path);
// }
//
// File[] files = file3.listFiles();
// for(File file1 :files){
// System.out.println(file1);
// }
//遍历D盘下所有的文件和目录?
// System.out.println("***************************");
// File[] roots = File.listRoots();
// for(File root :roots){
// System.out.println(root);
// }
// printFiles(new File("D:\\idea_workspace"));
// File file4 = new File("D:\\a.txt");
// file4.mkdir();
// file4.mkdirs();
// file4.renameTo(new File("a123.txt"));
// System.out.println(File.separator);
//删除目录?
File file5 = new File("D:\\b");
file5.delete();
}
/*
在遍历文件的时候,文件系统中可能存在一些受保护的文件,访问是需要访问权限的,此时会抛出空指针异常。
*/
public static void printFiles(File file){
//获取指定路径名下所有的文件和目录
File[] files = file.listFiles();
//遍历文件
for(File file1 : files){
//判断是否是目录
if(file1.isDirectory()){
printFiles(file1);
}else{
System.out.println(file1.getAbsolutePath());
}
}
}
}
二、WriterDemo的使用
1、WriterDemo
* 什么时候需要加flush,什么时候不加flush
* 最保险的方式,在输出流关闭之前每次都flush一下,然后再关闭
* 当某一个输出流对象中带有缓冲区的时候,就需要进行flush,不建议大家去记住每个输出流的分类
*
* */
public class WriterDemo {
public static void main(String[] args) {
File file = new File("a4.txt");
Writer writer = null;
try {
//创建字符流对象
writer = new FileWriter(file,false);
//传输数据
writer.write(99);
writer.write("abc你好,中国!");
writer.write(new char[]{'5','t','p'});
writer.append('a').append('b').append('c').append('d');
//强制将缓冲区的数据写入到目的地
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、CopyFileDemo的使用
2、CopyFileDemo
public class CopyFileDemo {
public static void main(String[] args) {
//创建数据源
File src = new File("D:\\a.docx");
//创建目标数据源
File dest = new File(src.getName());
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
//创建字符输入流对象
fileReader = new FileReader(src);
//创建字符输出流对象
fileWriter = new FileWriter(dest);
int length = 0;
char[] chars = new char[1024];
while((length = fileReader.read(chars))!=-1){
//写出数据到目标数据源
fileWriter.write(chars,0,length);
fileWriter.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、PrintStreamDemo的使用
1、PrintStreamDemo的使用
public class PrintStreamDemo {
public static void main(String[] args) {
PrintStream printStream = new PrintStream(System.out);
// PrintStream printStream = null;
// try {
// printStream = new PrintStream(new FileOutputStream("a3.txt"));
printStream.print("HelloWorld!");
printStream.println();
printStream.println(true);
//格式化输出 %s:字符串 %d:整数 %f:浮点类型
printStream.printf("%s----%d-----%.2f","hello",100,123.234);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } finally {
printStream.close();
// }
}
}
五、CharArrayReaderDemo的使用
1、CharArrayReaderDemo的使用
public class CharArrayReaderDemo {
public static void main(String[] args) {
String str = "www.baidu.com百度";
char[] chars = str.toCharArray();
CharArrayReader charArrayReader = new CharArrayReader(chars);
try {
// int read = charArrayReader.read();
// System.out.println((char)read);
int read = 0;
while((read = charArrayReader.read())!=-1){
System.out.print((char)read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
charArrayReader.close();
}
}
}
六、BufferedWriter的使用
1、BufferedWriterDemo
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter("d3.txt");
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(101);
bufferedWriter.write("www.baidu.com百度".toCharArray());
//写入一个换行
bufferedWriter.newLine();
bufferedWriter.write("今天天气不错!");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七、BufferedReader类
1、BufferedReaderDemo
public class BufferedReaderDemo {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader reader = null;
try {
fileReader = new FileReader("a1.txt");
reader= new BufferedReader(fileReader);
// int length = 0;
// char[] chars = new char[1024];
// while((length = reader.read(chars))!=-1){
// System.out.println(new String(chars,0,length));
// }
// String s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
// s = reader.readLine();
// System.out.println(s);
String str = "";
while((str = reader.readLine())!=null){
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
八、BufferedWriter的使用
1、BufferedWriterDemo
p
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter("d3.txt");
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(101);
bufferedWriter.write("www.baidu.com百度".toCharArray());
//写入一个换行
bufferedWriter.newLine();
bufferedWriter.write("今天天气不错!");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
九、从万维网上摘取百度网页的操作
1、BaiduDemo
public class BaiduDemo {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
bufferedWriter = new BufferedWriter(new FileWriter("D:\\baidu.html"));
String str = "";
while(( str = bufferedReader.readLine() )!=null){
bufferedWriter.write(str);
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}