欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java流的简介

发布时间:2025/3/19 java 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Java流的简介 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

java中包含了对文件的操作,包括对文件本身的操作,和对文件内容的操作

对文件本身的操作,主要在File类中封装。文件内容的操作,主要是字符流和字节流

import java.io.*;public class TestFile {public static void main(String[] args){//1.对文件本身的操作File file = new File("kangyucheng.txt");try {System.out.println("文件是否创建成功:"+file.createNewFile());System.out.println("文件是否存在:"+file.exists());System.out.println("文件父目录:"+file.getParent());System.out.println("文件目录"+file.getPath());System.out.println("文件能否执行"+file.canExecute());System.out.println("文件能否读"+file.canRead());System.out.println("文件能否写"+file.canWrite());} catch (IOException e) {e.printStackTrace();}//2.对文件内容的操作之一【字节流】String myBlogAddress = "https://blog.csdn.net/Kangyucheng ";//(1)写文件。try {//字节流写文件OutputStream outputStream = new FileOutputStream(file);outputStream.write(myBlogAddress.getBytes());outputStream.close();System.out.println("OutputStream写文件写入成功");//字符流写文件Writer writer = new FileWriter(file,true);//true,代表是追加往上写writer.write(myBlogAddress);writer.append(myBlogAddress);writer.close();System.out.println("Writer写文件写入成功");} catch (IOException e) {e.printStackTrace();}//(2)读文件try {InputStream inputStream = new FileInputStream(file);byte[] data=new byte[inputStream.available()];inputStream.read(data);System.out.println("读到文件:"+file.getPath()+"内容为:\n"+new String(data));} catch (IOException e) {e.printStackTrace();}} }

运行结果为:

总结

以上是生活随笔为你收集整理的Java流的简介的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。