java 文件内容读取到字符串中_Java将字符串写入文件与将文件内容读取到字符串...

这篇博客详细介绍了在Java中使用不同方法将字符串写入文件和从文件读取字符串的代码示例。包括使用PrintStream、FileWriter、PrintWriter、RandomAccessFile以及FileOutputStream等类进行写入,以及使用FileInputStream、FileReader、InputStreamReader和BufferedReader进行读取。这些方法涵盖了追加、覆盖、字节流和字符流等多种操作方式。

将字符串写入文件

方法一

public void WriteStringToFile(String filePath) {

try {

File file = new File(filePath);

PrintStream ps = new PrintStream(new FileOutputStream(file));

ps.println("http://www.jb51.net");// 往文件里写入字符串

ps.append("http://www.jb51.net");// 在已有的基础上添加字符串

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

方法二

public void WriteStringToFile2(String filePath) {

try {

FileWriter fw = new FileWriter(filePath, true);

BufferedWriter bw = new BufferedWriter(fw);

bw.append("在已有的基础上添加字符串");

bw.write("abc\r\n ");// 往已有的文件上添加字符串

bw.write("def\r\n ");

bw.write("hijk ");

bw.close();

fw.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

方法三

public void WriteStringToFile3(String filePath) {

try {

PrintWriter pw = new PrintWriter(new FileWriter(filePath));

pw.println("abc ");

pw.println("def ");

pw.println("hef ");

pw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

方法四

public void WriteStringToFile4(String filePath) {

try {

RandomAccessFile rf = new RandomAccessFile(filePath, "rw");

rf.writeBytes("op\r\n");

rf.writeBytes("app\r\n");

rf.writeBytes("hijklllll");

rf.close();

} catch (IOException e) {

e.printStackTrace();

}

}

方法五

public void WriteStringToFile5(String filePath) {

try {

FileOutputStream fos = new FileOutputStream(filePath);

String s = "http://www.jb51.netl";

fos.write(s.getBytes());

fos.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

将文件内容读取到字符串

方法一

/**

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

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

*/

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

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吧

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

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()

{

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类是从字节流到字符流的桥梁*/

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

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();

}

from:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值