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启
- 下一篇: Http协议简单介绍