⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createfile.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
字号:
package net.aetherial.gis.surface;

import java.io.*;

public class CreateFile {
  File f = null;
  private String encode = "UTF-8"; //default : UTF-8
  public CreateFile(File file) {
    this.f = file;
    this.createFile(this.f);
  }

  private boolean createFile(File file) {

    if (file.exists()) {
      return true;
    }
    else {
      file.getParentFile().mkdirs();
      try {
        file.createNewFile();
      }
      catch (IOException ex) {
        System.out.println("不能创建文件:" + file.getAbsolutePath() + " " +
                           ex.getMessage());
        return false;
      }

      return true;
    }
  }

  public void memoryToFile(String string) {
    try {
      if (f.isFile()) {
        f.getParentFile().mkdirs();
        f.createNewFile();
      }
      else {
//        throw new IOException("File对象为目录。");
      }
      System.out.println("File create:" + f.createNewFile());

      FileOutputStream fos = new FileOutputStream(f);
      //DataOutputStream dos = new DataOutputStream(fos);

      fos.write(string.getBytes(encode));
      //dos.flush();
      //dos.close();
      fos.flush();
      fos.close();
      //BufferedWriter bw = new BufferedWriter();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void copyFile(File source, File target) throws Exception {
    FileInputStream input;
    FileOutputStream output;
    int i = 0;
    input = new FileInputStream(source);
    output = new FileOutputStream(target);
    while(true){
      i = input.read();
      output.write(i);
    }
  }

  public void memoryToFile(byte[] b) {
    try {
      System.out.println("File create:" + f.createNewFile());
      FileOutputStream fos = new FileOutputStream(f);
      fos.write(b);
      fos.flush();
      fos.close();
    }
    catch (Exception e) {
//      e.printStackTrace();
    }
  }

  public void setEncode(String encoding) {
    this.encode = encoding;
  }

  public static void main(String agrs[]) {
    CreateFile cf = new CreateFile(new File("D://test1.txt"));
    cf.memoryToFile("wanga1@@@@o,s GPS test");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -