好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中读取文件转换为字符串的方法

方式一

/**

以字节为单位读取 文件 ,常用于读二进制文件,如图片、声音、影像等文件。

当然也是可以读 字符串 的。

*/

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

/* 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/

public string readstring1()

{

  try

  {

   //fileinputstream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 filereader。

   fileinputstream instream= this .openfileinput(file_name);

   bytearrayoutputstream bos = new bytearrayoutputstream();

   byte [] buffer= new byte [ 1024 ];

   int length=- 1 ;

   while ( (length = instream.read(buffer) != - 1 )

   {

    bos.write(buffer, 0 ,length);

    // .write方法 sdk 的解释是 writes count bytes from the byte array buffer starting at offset index to this stream.

    // 当流关闭以后内容依然存在

   }

   bos.close();

   instream.close();

   return bos.tostring();

   // 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"utf-8") 不更好么?

   // return new string(bos.tobytearray(),"utf-8"); 

  }

}

方式二

// 有人说了 filereader 读字符串更好,那么就用filereader吧

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

// 每次读一个是不是效率有点低了?

private static string readstring2()

{

  stringbuffer str= new stringbuffer( "" );

  file file= new file(file_in);

  try {

   filereader fr= new filereader(file);

   int ch = 0 ;

   while ((ch = fr.read())!=- 1 )

   {

    system.out.print(( char )ch+ " " );

   }

   fr.close();

  } catch (ioexception e) {

   // todo auto-generated catch block

   e.printstacktrace();

   system.out.println( "file reader出错" );

  }

  return str.tostring();

}

方式三

/按字节读取字符串/

/* 个人感觉最好的方式,(一次读完)读字节就读字节吧,读完转码一次不就好了*/

private static string readstring3()

{

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

string str= "" ;

file file= new file(file_in);

try {

  fileinputstream in= new fileinputstream(file);

  // size 为字串的长度 ,这里一次性读完

  int size=in.available();

  byte [] buffer= new byte [size];

  in.read(buffer);

  in.close();

  str= new string(buffer, "gb2312" );

} catch (ioexception e) {

  // todo auto-generated catch block

  return null ;

  e.printstacktrace();

}

return str;

}

方式四

/inputstreamreader+bufferedreader读取字符串 , inputstreamreader类是从字节流到字符流的桥梁/

?

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

/* 按行读对于要处理的格式化数据是一种读取的好方式 */

private static string readstring4()

{

  int len= 0 ;

  stringbuffer str= new stringbuffer( "" );

  file file= new file(file_in);

  try {

   fileinputstream is= new fileinputstream(file);

   inputstreamreader isr= new inputstreamreader(is);

   bufferedreader in= new bufferedreader(isr);

   string line= null ;

   while ( (line=in.readline())!= null )

   {

    if (len != 0 ) // 处理换行符的问题

    {

     str.append( "\r\n" +line);

    }

    else

    {

     str.append(line);

    }

    len++;

   }

   in.close();

   is.close();

  } catch (ioexception e) {

   // todo auto-generated catch block

   e.printstacktrace();

  }

  return str.tostring();

}

路要一步一步走,记住自己走过的路,不再犯同样的错误,才是真正的成长!欢迎指点、交流。

以上这篇java中读取文件 转换 为字符串的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/stimgo/article/details/52856570

查看更多关于Java中读取文件转换为字符串的方法的详细内容...

  阅读:63次