1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| package jdk.util.sourceCode;
import java.io.*;
public class TestIO {
public static void main(String[] args){
readWriteFile("F:\\game\\xx.mp4", "E:\\github-project\\jdk\\src\\main\\java\\jdk\\util\\sourceCode\\测试io.txt");
}
public static void createFile(String path, String fileName){ File file = new File(path+fileName); try { file.createNewFile(); System.out.println("目标文件已存在: " + path + fileName); } catch (IOException e) { e.printStackTrace(); } }
public static void deleteFile(String path, String fileName){ File file = new File(path+fileName); if(file.exists()){ file.delete(); System.out.println("目标文件已删除"); }else{ System.out.println("要删除的目标文件不存在"); } }
public static void createFolder(String path, String folderName){ File file = new File(path+folderName); file.mkdir(); System.out.println("该文件夹已经存在于: " + path + folderName); }
public static void listFiles(String path){ File file = new File(path); if (file.isDirectory()){ File[] fileArray = file.listFiles(); for (int i = 0; i < fileArray.length; i++){ System.out.println( "该目录下的文件: " + fileArray[i]); System.out.println( "该目录下的文件或文件夹的名字: " + fileArray[i].getName()); } }else{ System.out.println(path + " 目录不存在"); } }
public static void isFolder(String path, String folderName){ File file = new File(path + folderName); if (file.isDirectory()){ System.out.println(path + folderName + " 是一个目录"); }else{ System.out.println(path + folderName + " 不是一个目录"); } }
public static void writeFileByByte(String fileName){ File file = new File(fileName); OutputStream outputStream = null; try { outputStream = new FileOutputStream(file, true); } catch (FileNotFoundException e) { e.printStackTrace(); } int a = 12345678; byte[] b = new byte[]{ (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) ((a ) & 0xFF) }; try { outputStream.write(b); outputStream.close(); }catch (IOException e) { e.printStackTrace(); } }
public static void readFileByByte(String fileName){ File file = new File(fileName); InputStream inputStream = null; try { inputStream = new FileInputStream(file); byte[] byter = new byte[1024]; int len = inputStream.read(byter); inputStream.close(); System.out.println(new String(byter, 0, len)); } catch (Exception e) { e.printStackTrace(); } }
public static void readWriteFile(String sourceFile, String desFile){ File inputFile = new File(sourceFile); File outputFile = new File(desFile); InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = new FileInputStream(inputFile); byte[] byter = new byte[1024]; inputStream.read(byter); outputStream = new FileOutputStream(outputFile, true); outputStream.write(byter); outputStream.close(); inputStream.close(); System.out.println("操作完成"); }catch (Exception e){ System.out.println(e.getMessage()); } }
}
|