zoukankan      html  css  js  c++  java
  • FileOutputStream

        /* 使用FileOutputStream写入文件,FileOutputStream的write() 方法只接受byte[] 类型 
           的参数,所以需要将string通过getBytes()方法转换为字节数组。 
        1、首先判断文件是否存在,不存在就新建一个 
        2、写入文件是以覆盖方式 
        3、文件不存在会自动创建,存在则会被重写 
         */  
          
        import java.io.*;  
          
        public class Exercise {  
            
          public static void main(String args[]) {  
            FileOutputStream fos = null;  
            File file;  
            String mycontent = "This is my Data which needs to be written into the file.";  
            try {  
              file = new File("/home/zjz/Desktop/myFile.txt");  
              fos = new FileOutputStream(file);  
              byte[] bytesArray = mycontent.getBytes();  
              fos.write(bytesArray);  
              fos.flush();  
              System.out.println("File Written Successfully");  
            } catch (FileNotFoundException e) {  
              e.printStackTrace();  
            } catch (IOException e) {  
              e.printStackTrace();  
            } finally {  
              try {  
                if (fos != null) {  
                  fos.close();  
                }  
              } catch (IOException ioe) {  
                System.out.println("Error in closing the Stream");  
              }  
            }  
          }  
          
        }  
  • 相关阅读:
    Vue 介绍
    Django 组件-分页器
    Django 组件content_type
    DRF 解析器组件
    Django
    Django 组件-ModelForm
    Django 组件-用户认证
    Django 组件-中间件
    Django 组件-cookie与session
    Django CBV与FBV
  • 原文地址:https://www.cnblogs.com/xwb583312435/p/9015381.html
Copyright © 2011-2022 走看看