国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 基礎知識之IO總結

java 基礎知識之IO總結

2020-08-25 10:33Java之家 Java教程

這篇文章主要介紹了java 基礎知識之IO總結的相關資料,Java中的I/O分為兩種類型,一種是順序讀取,一種是隨機讀取,需要的朋友可以參考下

java  基礎知識之io總結

     我計劃在接下來的幾篇文章中快速回顧一下java,主要是一些基礎的jdk相關的內容。

  工作后,使用的技術隨著項目的變化而變化,時而c#,時而java,當然還有其他一些零碎的技術。總體而言,c#的使用時間要更長一些,其次是java。我本身對語言沒有什么傾向性,能干活的語言,就是好語言。而且從面向對象的角度來看,我覺得c#和java對我來說,沒什么區別。

  這篇文章主要回顧java中和i/o操作相關的內容,i/o也是編程語言的一個基礎特性,java中的i/o分為兩種類型,一種是順序讀取,一種是隨機讀取。

  我們先來看順序讀取,有兩種方式可以進行順序讀取,一種是inputstream/outputstream,它是針對字節進行操作的輸入輸出流;另外一種是reader/writer,它是針對字符進行操作的輸入輸出流。

  下面我們畫出inputstream的結構

  java 基礎知識之IO總結

  1. fileinputstream:操作文件,經常和bufferedinputstream一起使用
  2. pipedinputstream:可用于線程間通信
  3. objectinputstream:可用于對象序列化
  4. bytearrayinputstream:用于處理字節數組的輸入
  5. linenumberinputstream:可輸出當前行數,并且可以在程序中進行修改

  下面是outputstream的結構

  java 基礎知識之IO總結

printstream:提供了類似print和println的接口去輸出數據

  下面我們來看如何使用stream的方式來操作輸入輸出

使用inputstream讀取文件

?
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
使用fileinputstream讀取文件信息
public static byte[] readfilebyfileinputstream(file file) throws ioexception
{
  bytearrayoutputstream output = new bytearrayoutputstream();
  fileinputstream fis = null;
  try
  {
    fis = new fileinputstream(file);
    byte[] buffer = new byte[1024];
    int bytesread = 0;
    while((bytesread = fis.read(buffer, 0, buffer.length)) != -1)
    {
      output.write(buffer, 0, bytesread);
    }
  }
  catch(exception ex)
  {
    system.out.println("error occurs during reading " + file.getabsolutefile());
  }
  finally
  {
    if (fis !=null) fis.close();
    if (output !=null) output.close();
  }
  return output.tobytearray();
}

 

?
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
使用bufferedinputstream讀取文件
public static byte[] readfilebybufferedinputstream(file file) throws exception
{
  fileinputstream fis = null;
  bufferedinputstream bis = null;
  bytearrayoutputstream output = new bytearrayoutputstream();
  try
  {
    fis = new fileinputstream(file);
    bis = new bufferedinputstream(fis);
    byte[] buffer = new byte[1024];
    int bytesread = 0;
    while((bytesread = bis.read(buffer, 0, buffer.length)) != -1)
    {
      output.write(buffer, 0, bytesread);
    }
  }
  catch(exception ex)
  {
    system.out.println("error occurs during reading " + file.getabsolutefile());
  }
  finally
  {
    if (fis != null) fis.close();
    if (bis != null) bis.close();
    if (output != null) output.close();
  }
  return output.tobytearray();
}

使用outputstream復制文件

?
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
使用fileoutputstream復制文件
public static void copyfilebyfileoutputstream(file file) throws ioexception
{
  fileinputstream fis = null;
  fileoutputstream fos = null;
  try
  {
    fis = new fileinputstream(file);
    fos = new fileoutputstream(file.getname() + ".bak");
    byte[] buffer = new byte[1024];
    int bytesread = 0;
    while((bytesread = fis.read(buffer,0,buffer.length)) != -1)
    {
      fos.write(buffer, 0, bytesread);
    }
    fos.flush();
  }
  catch(exception ex)
  {
    system.out.println("error occurs during copying " + file.getabsolutefile());
  }
  finally
  {
    if (fis != null) fis.close();
    if (fos != null) fos.close();
  }
}

 

?
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
使用bufferedoutputstream復制文件
public static void copyfilebybufferedoutputstream(file file)throws ioexception
{
  fileinputstream fis = null;
  bufferedinputstream bis = null;
  fileoutputstream fos = null;
  bufferedoutputstream bos = null;
  try
  {
    fis = new fileinputstream(file);
    bis = new bufferedinputstream(fis);
    fos = new fileoutputstream(file.getname() + ".bak");
    bos = new bufferedoutputstream(fos);
    byte[] buffer = new byte[1024];
    int bytesread = 0;
    while((bytesread = bis.read(buffer, 0, buffer.length)) != -1)
    {
      bos.write(buffer, 0, bytesread);
    }
    bos.flush();
  }
  catch(exception ex)
  {
    system.out.println("error occurs during copying " + file.getabsolutefile());
  }
  finally
  {
    if (fis != null) fis.close();
    if (bis != null) bis.close();
    if (fos != null) fos.close();
    if (bos != null) bos.close();
  }
}

這里的代碼對異常的處理非常不完整,稍后我們會給出完整嚴謹的代碼。

  下面我們來看reader的結構

  java 基礎知識之IO總結

  這里的reader基本上和inputstream能夠對應上。  

  writer的結構如下

  java 基礎知識之IO總結

  下面我們來看一些使用reader或者writer的例子

使用reader讀取文件內容

?
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
使用bufferedreader讀取文件內容
public static string readfile(string file)throws ioexception
{
  bufferedreader br = null;
  stringbuffer sb = new stringbuffer();
  try
  {
    br = new bufferedreader(new filereader(file));
    string line = null;
    
    while((line = br.readline()) != null)
    {
      sb.append(line);
    }
  }
  catch(exception ex)
  {
    system.out.println("error occurs during reading " + file);
  }
  finally
  {
    if (br != null) br.close();
  }
  return sb.tostring();
}

使用writer復制文件

?
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
使用bufferedwriter復制文件
public static void copyfile(string file) throws ioexception
{
  bufferedreader br = null;
  bufferedwriter bw = null;
  try
  {
    br = new bufferedreader(new filereader(file));
    bw = new bufferedwriter(new filewriter(file + ".bak"));
    string line = null;
    while((line = br.readline())!= null)
    {
      bw.write(line);
    }
  }
  catch(exception ex)
  {
    system.out.println("error occurs during copying " + file);
  }
  finally
  {
    if (br != null) br.close();
    if (bw != null) bw.close();
  }
}

  下面我們來看如何對文件進行隨機訪問,java中主要使用randomaccessfile來對文件進行隨機操作。

創建一個大小固定的文件

?
1
2
3
4
5
6
7
8
創建大小固定的文件
public static void createfile(string file, int size) throws ioexception
{
  file temp = new file(file);
  randomaccessfile raf = new randomaccessfile(temp, "rw");
  raf.setlength(size);
  raf.close();
}

向文件中隨機寫入數據

?
1
2
3
4
5
6
7
8
向文件中隨機插入數據
public static void writefile(string file, byte[] content, int startpos, int contentlength) throws ioexception
{
  randomaccessfile raf = new randomaccessfile(new file(file), "rw");
  raf.seek(startpos);
  raf.write(content, 0, contentlength);
  raf.close();
}

  接下里,我們來看一些其他的常用操作

移動文件

?
1
2
3
4
5
6
7
8
9
移動文件
public static boolean movefile(string sourcefile, string destfile)
{
  file source = new file(sourcefile);
  if (!source.exists()) throw new runtimeexception("source file does not exist.");
  file dest = new file(destfile);
  if (!(new file(dest.getpath()).exists())) new file(dest.getparent()).mkdirs();
  return source.renameto(dest);
}

復制文件

?
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
復制文件
public static void copyfile(string sourcefile, string destfile) throws ioexception
{
  file source = new file(sourcefile);
  if (!source.exists()) throw new runtimeexception("file does not exist.");
  if (!source.isfile()) throw new runtimeexception("it is not file.");
  if (!source.canread()) throw new runtimeexception("file cound not be read.");
  file dest = new file(destfile);
  if (dest.exists())
  {
    if (dest.isdirectory()) throw new runtimeexception("destination is a folder.");
    else
    {
      dest.delete();
    }
  }
  else
  {
    file parentfolder = new file(dest.getparent());
    if (!parentfolder.exists()) parentfolder.mkdirs();
    if (!parentfolder.canwrite()) throw new runtimeexception("destination can not be written.");
  }
  fileinputstream fis = null;
  fileoutputstream fos = null;
  try
  {
    fis = new fileinputstream(source);
    fos = new fileoutputstream(dest);
    byte[] buffer = new byte[1024];
    int bytesread = 0;
    while((bytesread = fis.read(buffer, 0, buffer.length)) != -1)
    {
      fos.write(buffer, 0, bytesread);
    }
    fos.flush();
  }
  catch(ioexception ex)
  {
    system.out.println("error occurs during copying " + sourcefile);
  }
  finally
  {
    if (fis != null) fis.close();
    if (fos != null) fos.close();
  }
}

復制文件夾

?
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
復制文件夾
public static void copydir(string sourcedir, string destdir) throws ioexception
{
  
  file source = new file(sourcedir);
  if (!source.exists()) throw new runtimeexception("source does not exist.");
  if (!source.canread()) throw new runtimeexception("source could not be read.");
  file dest = new file(destdir);
  if (!dest.exists()) dest.mkdirs();
  
  file[] arrfiles = source.listfiles();
  for(int i = 0; i < arrfiles.length; i++)
  {
    if (arrfiles[i].isfile())
    {
      bufferedreader reader = new bufferedreader(new filereader(arrfiles[i]));
      bufferedwriter writer = new bufferedwriter(new filewriter(destdir + "/" + arrfiles[i].getname()));
      string line = null;
      while((line = reader.readline()) != null) writer.write(line);
      writer.flush();
      reader.close();
      writer.close();
    }
    else
    {
      copydir(sourcedir + "/" + arrfiles[i].getname(), destdir + "/" + arrfiles[i].getname());
    }
  }
}

刪除文件夾

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
刪除文件夾
public static void del(string filepath)
{
  file file = new file(filepath);
  if (file == null || !file.exists()) return;
  if (file.isfile())
  {
    file.delete();
  }
  else
  {
    file[] arrfiles = file.listfiles();
    if (arrfiles.length > 0)
    {
      for(int i = 0; i < arrfiles.length; i++)
      {
        del(arrfiles[i].getabsolutepath());
      }
    }
    file.delete();
  }
}

獲取文件夾大小

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
獲取文件夾大小
public static long getfoldersize(string dir)
{
  long size = 0;
  file file = new file(dir);
  if (!file.exists()) throw new runtimeexception("dir does not exist.");
  if (file.isfile()) return file.length();
  else
  {
    string[] arrfilename = file.list();
    for (int i = 0; i < arrfilename.length; i++)
    {
      size += getfoldersize(dir + "/" + arrfilename[i]);
    }
  }
  
  return size;
}

將大文件切分為多個小文件

?
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
將大文件切分成多個小文件
public static void splitfile(string filepath, long unit) throws ioexception
{
  file file = new file(filepath);
  if (!file.exists()) throw new runtimeexception("file does not exist.");
  long size = file.length();
  if (unit >= size) return;
  int count = size % unit == 0 ? (int)(size/unit) : (int)(size/unit) + 1;
  string newfile = null;
  fileoutputstream fos = null;
  fileinputstream fis =null;
  byte[] buffer = new byte[(int)unit];
  fis = new fileinputstream(file);
  long startpos = 0;
  string countfile = filepath + "_count";
  printwriter writer = new printwriter(new filewriter( new file(countfile)));
  writer.println(filepath + "\t" + size);
  for (int i = 1; i <= count; i++)
  {
    newfile = filepath + "_" + i;
    startpos = (i - 1) * unit;
    system.out.println("creating " + newfile);
    fos = new fileoutputstream(new file(newfile));
    int bytesread = fis.read(buffer, 0, buffer.length);
    if (bytesread != -1)
    {
      fos.write(buffer, 0, bytesread);
      writer.println(newfile + "\t" + startpos + "\t" + bytesread);
    }
    fos.flush();
    fos.close();
    system.out.println("startpos:" + i*unit + "; endpos:" + (i*unit + bytesread));
  }
  writer.flush();
  writer.close();
  fis.close();
}

將多個小文件合并為一個大文件

?
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
將多個小文件合并成一個大文件
public static void linkfiles(string countfile) throws ioexception
{
  file file = new file(countfile);
  if (!file.exists()) throw new runtimeexception("count file does not exist.");
  bufferedreader reader = new bufferedreader(new filereader(file));
  string line = reader.readline();
  string newfile = line.split("\t")[0];
  long size = long.parselong(line.split("\t")[1]);
  randomaccessfile raf = new randomaccessfile(newfile, "rw");
  raf.setlength(size);
  fileinputstream fis = null;
  byte[] buffer = null;
  
  while((line = reader.readline()) != null)
  {
    string[] arrinfo = line.split("\t");
    fis = new fileinputstream(new file(arrinfo[0]));
    buffer = new byte[integer.parseint(arrinfo[2])];
    long startpos = long.parselong(arrinfo[1]);
    fis.read(buffer, 0, integer.parseint(arrinfo[2]));
    raf.seek(startpos);
    raf.write(buffer, 0, integer.parseint(arrinfo[2]));
    fis.close();
  }
  raf.close();
}

執行外部命令

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
執行外部命令
public static void execexternalcommand(string command, string argument)
{
  process process = null;
  try
  {
    process = runtime.getruntime().exec(command + " " + argument);
    inputstream is = process.getinputstream();
    bufferedreader br = new bufferedreader(new inputstreamreader(is));
    string line = null;
    while((line = br.readline()) != null)
    {
      system.out.println(line);
    }
  }
  catch(exception ex)
  {
    system.err.println(ex.getmessage());
  }
  finally
  {
    if (process != null) process.destroy();
  }
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线观看中文字幕 | 欧美一区国产一区 | 亚洲色图二区 | 亚洲综合自拍 | 国产高清视频 | 成人欧美一区二区三区在线观看 | 天天精品 | 日韩免费在线视频 | 久色视频在线观看 | 国产精品伦理一区二区 | 视频四区| 国产精品初高中精品久久 | 免费成人高清在线视频 | 午夜精品一区 | www.亚洲成人 | 夜夜av | 一区二区三区日韩在线 | 午夜一区二区三区 | 一区二区三区视频在线观看 | 91精品一区二区三区久久久久久 | 欧美日韩第一页 | 青青草99 | 中文字幕在线视频观看 | 亚洲精品久久久久久动漫 | 午夜在线视频 | 亚洲精品一区二三区不卡 | 黑人粗大视频 | 日韩操操 | 亚洲国产精品一区二区三区 | 欧美一级在线观看 | 日韩在线视频观看 | 欧美中文字幕在线 | 蜜桃精品在线 | 国产成人亚洲精品 | 欧美一区二区三区久久 | 国产免费一区二区三区 | 欧美日韩在线免费观看 | 国产成人免费视频网站高清观看视频 | 97久久香蕉国产线看观看 | 国产高清在线精品一区二区三区 | 夜夜操天天干, |