1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package J20250725;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo02_FileOutputStream
- * @description
- * @create 2025/7/25
- */
- public class Demo02_FileOutputStream {
- public static void main(String[] args) throws IOException {
- FileOutputStream fos = null;
- //append参数 表示是否能够追加数据,true为是
- fos = new FileOutputStream("Z:/2025年7月25日.txt", true);
- String s = "你好,世界!!!";
- byte[] bytes = s.getBytes();
- fos.write(bytes);
- byte[] bytes1 = "12345678910".getBytes();
- fos.write(bytes1, 3, 5);
- fos.write("今天".getBytes());
- fos.write("\r\n".getBytes()); //Windows系统换行 \r\n
- fos.write("天儿挺好".getBytes());
- fos.close();
- }
- /*
- public static void main(String[] args){
- FileOutputStream fos=null;
- try {
- //append参数 表示是否能够追加数据,true为是
- fos=new FileOutputStream("Z:/2025年7月25日.txt",true);
- String s="你好,世界!!!";
- byte[] bytes = s.getBytes();
- fos.write(bytes);
- byte[] bytes1 = "12345678910".getBytes();
- fos.write(bytes1,3,5);
- fos.write("今天".getBytes());
- fos.write("\r\n".getBytes()); //Windows系统换行 \r\n
- fos.write("天儿挺好".getBytes());
- } catch (FileNotFoundException e) { //先处理文件找不到异常
- System.out.println("文件找不到了,我们要再这里进行相关的处理,找到文件");
- }catch (IOException e){ //再处理IO异常
- System.out.println("文件找到了,但是没有权限向文件读写数据,这里我们要做相关处理");
- }finally {
- //关闭流
- try {
- fos.close();
- } catch (IOException e) { //处理IO异常
- System.out.println("关闭流的时候,IO异常发生了,我们这里要做相关处理保证关闭流或做其他操作");
- }catch (Exception e){
- System.out.println("出现异常了.....我也不知道什么异常,反正要处理");
- }
- }
- //程序后续要执行的代码.......
- for (int i = 0; i < 1000; i++) {
- System.out.println("Hello "+i);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- */
- }
|