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
| public class FileInputStreamText01 { public static void main(String[] args) { FileInputStream fis = null; try { fis = new FileInputStream("D:\\Text\\联系.txt");
int FileData = fis.read(); System.out.println(FileData); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
} }
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
public class FileOutputStreamText01 { public static void main(String[] args) { FileOutputStream fos = null; try { fos = new FileOutputStream("D:\\Text\\联系.txt",true); String s = "我爱你,但是我现在需要变得更好!!!"; byte[] byt = s.getBytes(); fos.write(byt); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public class BufferReaderText02 { public static void main(String[] args) { FileInputStream fileInputStream = null; try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\\\Text\\\\联系.txt")));
String s1 = null; while ((s1 = bufferedReader.readLine()) != null){ System.out.println(s1); }
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
} }
public class BufferWriterText01 { public static void main(String[] args) { FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("D:\\Text\\java01.text",true); OutputStreamWriter outputStream = new OutputStreamWriter(fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStream); bufferedWriter.write("HEllo,world\n"); bufferedWriter.write("Number N0.1\n");
bufferedWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public class IoCopyText01 { public static void main(String[] args) { FileOutputStream fio = null; FileInputStream fis = null; try { fis = new FileInputStream("C:\\Users\\11026\\Desktop\\1611539565799.docx"); fio = new FileOutputStream("D:\\Text\\1611539565799.docx");
byte[] byt = new byte[1024 * 1024]; int count = 0; while((count = fis.read(byt)) != -1){ fio.write(byt,0,count); }
fio.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fio != null) { try { fio.close(); } catch (IOException e) { e.printStackTrace(); } } }
} }
public class CopyText02 { public static void main(String[] args) { FileWriter in = null; FileReader out = null; try { in = new FileWriter("D:\\Text\\NEWS.txt"); out = new FileReader("D:\\NEWS.txt"); char[] chars = new char[4]; int readCount = 0; while((readCount = out.read(chars)) != -1){ in.write(chars,0,readCount); } in.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|