对于Java中*流* 的认识

来源:互联网 发布:java入门中文书籍 编辑:程序博客网 时间:2024/09/21 09:04

Java中对文件的操作是以流的方式进行的。流是指一连串流动的字符,是以先进先出方式发送信息的通道。

文件的基本属性在Java中对应的方法:

File f = new File("aaa.txt");
System.out.println("文件大小:"+f.length());
System.out.println("文件名:"+f.getName());
System.out.println("是文件:"+f.isFile());
System.out.println("是文件夹:"+f.isDirectory());
System.out.println("是可读:"+f.canRead());
System.out.println("是可写:"+f.canWrite());
System.out.println("是隐藏:"+f.isHidden());
System.out.println("文件是否存在:"+f.exists());
System.out.println("文件的绝对路径:"+f.getAbsolutePath());
System.out.println("文件的相对路径:"+f.getPath());

************************************************************************************************************

public class ShowDir {public static void main(String[] args) {//遍历D盘的所有文件和文件夹File f = new File("d:");show(f,"");}private static void show(File f ,String path) {File[] fs = f.listFiles();for (File file : fs) {System.out.println(path + file.getName());if(file.isDirectory()){show(file, path+"-");}}}}
流按照方向分输入流(读)和输出流(写)

************************************************************************************************************

输入流(读):input——read

public class TestInput {public static void main(String[] args) throws IOException {//通过输入流读取文件try {FileInputStream fis = new FileInputStream("d:/javach11/aaa.txt");byte[] bs = new byte[fis.available()];fis.read(bs);for (int i = 0; i < bs.length; i++) {System.out.print((char)bs[i]);}fis.close();} catch (Exception e) {e.printStackTrace();}}}

************************************************************************************************************

输入流(读):output——write

public class TestOutput {public static void main(String[] args) throws IOException {//通过输出流在文件中写内容FileOutputStream fos = new FileOutputStream("D:/workspace/javach11/aaa.txt",true);String str = "aaaaaaaaaaaaaaaaaaaaaaaaa";byte[] bs = str.getBytes(); fos.write(bs);fos.close();}}

************************************************************************************************************

流按照内容分字节流(byte)和字符流(char)

************************************************************************************************************

public class TestChar {public static void main(String[] args) throws IOException {//涉及到中文字符的读写时用字符流FileWriter fw = new FileWriter("D:/aaa.txt",true);BufferedWriter bw = new BufferedWriter(fw);bw.newLine();bw.write("你好");bw.newLine();bw.write("出去吗");bw.write("不出去");bw.close();fw.close();FileReader fr = new FileReader("D:/aaa.txt");BufferedReader br= new BufferedReader(fr);String line = null;while ((line = br.readLine()) != null) {System.out.println(line);}br.close();fr.close();}}

************************************************************************************************************

public class TestSysOut {public static void main(String[] args) throws IOException{//通过控制台书写内容到某个文件FileOutputStream fos = new FileOutputStream("D:/workspace/javach11/aaa.txt");PrintStream ps = new PrintStream(fos);System.setOut(ps);System.out.println("这是由java,System.out.println()输出的内容");System.out.println("看看换行好使不");ps.close();fos.close();}}

************************************************************************************************************

一般情况下,通过类名来判断流的类型。

类名中Stream的都是字节流,有Writer或Reader的是字符流。

InputReader都是输入流,

OutputWriter是输出流。

0 0
原创粉丝点击