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();}} }运行结果为:
总结
- 上一篇: 在Opendaylight中karaf启
- 下一篇: Java入门超简单程序Song List