对于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();}}}
************************************************************************************************************
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的是字符流。
Input、Reader都是输入流,
Output、Writer是输出流。
- 对于Java中*流* 的认识
- 对于Java的认识
- 对于java注解的认识
- 关于Java中对于jsp文件的浅认识?
- 对于 scanner输入流中next() nextline()的认识
- 对于php中magic_quotes_gpc的一点认识
- 对于项目中路径的认识
- 对于linux中管道的认识
- 对于java类加载器的认识
- 对于Portlet的认识
- 对于SSH的认识
- 对于UIWindow的认识
- 对于ajax的认识
- 对于计算机语言的认识
- 对于ajax的认识
- 对于MVC的认识
- 对于SQLite的认识
- 数据结构中串部分,对于一些问题的认识
- 浅谈Java项目配置
- 1020. Tree Traversals (25)
- Redis在集群环境中生成唯一ID
- 【软考复习】——面向对象
- 多态 /java初学
- 对于Java中*流* 的认识
- 设计模式-结构型- 适配器模式(Adapter)
- git理解
- PopupWindow的基本用法
- Why doesn't Genymotion run on Windows 10?
- Java中类的加载与反射机制
- 260. Single Number III
- java虚拟机内存模型
- 抽象类 /java初学