Java 文件流
字节流和字符流 输入 / 输出(I / O)是内存和物理设备之间的操作。输入正在将数据从物理设备复制到内存。输出正在将数据从内存复制到设备。IO 操作通过流执行。流链接到物理设备,例如磁盘文件,键盘,网络套接字。
Java 读写文件的 IO 流分两大类:
字节流: 基类:Reader 和 Writer
字符流: 基类:InputStream 和 OutputStream
BufferedInputStream 是带缓冲区的输入流,默认缓冲区大小是 8M,能够减少访问磁盘的次数,提高文件读取性能; BufferedOutputStream 是带缓冲区的输出流,能够提高文件的写入效率。 BufferedInputStream 与 BufferedOutputStream 分别是 FilterInputStream 类和 FilterOutputStream 类的子类,实现了装饰设计模式。
示例(Java) 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 public class MainActivity extends AppCompatActivity { private String inPath = Environment.getExternalStorageDirectory()+"/a.txt" ; private String outPath = Environment.getExternalStorageDirectory()+"/text.txt" ; private String dataPath ; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); if (ContextCompat.checkSelfPermission(this , Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this , Manifest.permission.WRITE_EXTERNAL_STORAGE)) { initData(); } else { ActivityCompat.requestPermissions(this , new String []{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11 ); } } } @Override public void onRequestPermissionsResult (int requestCode, String permissions[], int [] grantResults) { switch (requestCode) { case 11 : { if (grantResults.length > 0 && grantResults[0 ] == PackageManager.PERMISSION_GRANTED) { initData(); } else { } return ; } } } private void initData () { } private void flowPath (String filePath) { File file = new File (filePath); FileReader fileReader ; BufferedReader bufferedReader ; StringBuffer stringBuffer = new StringBuffer (); try { fileReader = new FileReader (file); bufferedReader = new BufferedReader (fileReader); String str ; while ((str = bufferedReader.readLine()) != null ) { stringBuffer.append(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void byteStream (String filePath) { File file = new File (filePath); InputStream inputStream = null ; OutputStream outputStream = null ; try { inputStream = new FileInputStream (file); outputStream = new FileOutputStream (outPath); int temp ; while ((temp = inputStream.read())!= -1 ){ outputStream.write(temp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream !=null && outputStream!=null ){ try { inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void characterStream (String filePath) { File file = new File (filePath); FileReader fileReader = null ; FileWriter fileWriter = null ; try { fileReader = new FileReader (file); fileWriter = new FileWriter (outPath); int temp ; while ((temp = fileReader.read())!=-1 ){ fileWriter.write((char )temp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fileReader!=null && fileWriter!=null ){ try { fileReader.close(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void readFileByLine (String inPath) { File file = new File (inPath); BufferedReader bufReader = null ; BufferedWriter bufWriter = null ; try { bufReader = new BufferedReader (new FileReader (file)); bufWriter = new BufferedWriter (new FileWriter (outPath)); String temp = null ; while ((temp = bufReader.readLine()) != null ) { bufWriter.write(temp+"\n" ); } } catch (Exception e) { e.getStackTrace(); } finally { if (bufReader != null && bufWriter != null ) { try { bufReader.close(); bufWriter.close(); } catch (IOException e) { e.getStackTrace(); } } } } public String getString (String inPath) { InputStream inputStream = null ; BufferedReader bufferedReader = null ; try { inputStream=new FileInputStream (new File (inPath)); InputStreamReader inputStreamReader = new InputStreamReader (inputStream, "UTF-8" ); bufferedReader = new BufferedReader (inputStreamReader); StringBuffer sb = new StringBuffer (); String line; while ((line = bufferedReader.readLine()) != null ) { sb.append(line); sb.append("\n" ); } return sb.toString(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (inputStream != null && bufferedReader != null ) { try { inputStream.close(); bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return null ; } private String transform (String inPath) { InputStream inputStream = null ; BufferedReader bufferedReader = null ; try { URL url = new URL ("https://www.baidu.com/" ); URLConnection urlconnnection = url.openConnection(); inputStream = urlconnnection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader (inputStream, "GB2312" ); bufferedReader = new BufferedReader (inputStreamReader); StringBuffer webContent = new StringBuffer (); String str = null ; while ((str = bufferedReader.readLine()) != null ) { webContent.append(str); } int ipStart = webContent.indexOf("[" ) + 1 ; int ipEnd = webContent.indexOf("]" ); return webContent.substring(ipStart, ipEnd); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null && bufferedReader != null ) { try { inputStream.close(); bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return null ; } public boolean saveFile (String inPath) { File file = new File (inPath); try { FileOutputStream fos = new FileOutputStream (file); fos.write(("zhouzhou 222" ).getBytes()); fos.flush(); fos.close(); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public static void deleteFiles (File file) { if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0 ; i < files.length; i++) { File f = files[i]; deleteFiles(f); } file.delete(); } else if (file.exists()) { file.delete(); }} private void initView () { } }
示例(Kotlin) Kotlin 在执行 I / O 时继承 Java,支持两种类型的流:字节和字符。字节流 用于处理字节数据,例如二进制数据。字符流 用于处理字符数据,例如文本文件。
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 class FileActivity :BaseActivity () { override fun onCreate (savedInstanceState: Bundle ?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_file) val inputText = load() if (inputText.isNotEmpty()){ et.setText(inputText) et.setSelection(inputText.length) Toast.makeText(this ,"Restoring succeeded" ,Toast.LENGTH_SHORT).show() } } private fun load () : String { val content = StringBuilder() try { val input = openFileInput("data" ) val reader = BufferedReader(InputStreamReader(input)) reader.use{ reader.forEachLine { content.append(it) } } }catch (e: IOException){ e.printStackTrace() } return content.toString() } private fun save (inputText:String ) { try { val output = openFileOutput("data" ,Context.MODE_PRIVATE) val writer = BufferedWriter(OutputStreamWriter(output)) writer.use { it.write(inputText) } }catch (e: IOException){ e.printStackTrace() } } override fun onDestroy () { super .onDestroy() val inputText = et.text.toString() save(inputText) } companion object { fun actionStart (context: Context ) { val intent = Intent(context, FileActivity::class .java) context.startActivity(intent) } } }
Java 随机访问文件
使用文件输入和输出流的读取和写入是顺序过程。 使用随机访问文件,我们可以在文件中的任何位置读取或写入。
RandomAccessFile 类的一个对象可以进行随机文件访问。我们可以读 / 写字节和所有原始类型的值到一个文件。 RandomAccessFile 可以使用其 readUTF () 和 writeUTF () 方法处理字符串。 RandomAccessFile 类不在 InputStream 和 OutputStream 类的类层次结构中。
模式 可以在四种不同的访问模式中创建随机访问文件。访问模式值是一个字符串:
模式
含义
“r”
文件以只读模式打开。
“rw”
该文件以读写模式打开。 如果文件不存在,则创建该文件。
“rws”
该文件以读写模式打开。 对文件的内容及其元数据的任何修改立即被写入存储设备。
“rwd”
该文件以读写模式打开。 对文件内容的任何修改立即写入存储设备。
读和写 1 2 3 4 RandomAccessFile raf = new RandomAccessFile ("randomtest.txt" , "rw" );
随机访问文件具有文件指针,当我们从其读取数据或向其写入数据时,该文件指针向前移动。 文件指针是我们下一次读取或写入将开始的光标。 其值指示光标与文件开头的距离(以字节为单位)。 我们可以通过使用其 getFilePointer () 方法来获取文件指针的值。 当我们创建一个 RandomAccessFile 类的对象时,文件指针被设置为零。 我们可以使用 seek () 方法将文件指针设置在文件中的特定位置。 RandomAccessFile 的 length()方法返回文件的当前长度。我们可以通过使用其 setLength () 方法来扩展或截断文件。
示例 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 private void readRandomAccessFile (String inPath) { String fileName = inPath; File fileObject = new File (inPath); if (!fileObject.exists()) { try { initialWrite(fileName); } catch (IOException e) { e.printStackTrace(); } } try { readFile(fileName); readFile(fileName); } catch (IOException e) { e.printStackTrace(); } } public static void readFile (String fileName) throws IOException { RandomAccessFile raf = new RandomAccessFile (fileName, "rw" ); int counter = raf.readInt(); String msg = raf.readUTF(); Log.e("TAG" ,"counter:" +counter); Log.e("TAG" ,"msg:" +msg); incrementReadCounter(raf); raf.close(); } public static void incrementReadCounter (RandomAccessFile raf) throws IOException { long currentPosition = raf.getFilePointer(); raf.seek(0 ); int counter = raf.readInt(); counter++; raf.seek(0 ); raf.writeInt(counter); raf.seek(currentPosition); } public static void initialWrite (String fileName) throws IOException { RandomAccessFile raf = new RandomAccessFile (fileName, "rw" ); raf.writeInt(0 ); raf.writeUTF("Hello world!" ); raf.close(); }
链接
传送门 :GitHub
预览: