/***1 按字节写入 FileOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void outputStreamTest(intcount, String str) {

File f= new File("f:test1.txt");

OutputStream os= null;try{

os= newFileOutputStream(f);for (int i = 0; i < count; i++) {

os.write(str.getBytes());

}

os.flush();

System.out.println("file's long:" +f.length());

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

os.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***2 按字节缓冲写入 BufferedOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void bufferedOutputTest(intcount, String str) {

File f= new File("f:test2.txt");

BufferedOutputStream bos= null;try{

OutputStream os= newFileOutputStream(f);

bos= newBufferedOutputStream(os);for (int i = 0; i < count; i++) {

bos.write(str.getBytes());

}

bos.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***3 按字符写入 FileWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void fileWriteTest(intcount, String str) {

File f= new File("f:test.txt");

Writer writer= null;try{

writer= newFileWriter(f);for (int i = 0; i < count; i++) {

writer.write(str);

}

writer.flush();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

writer.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}/***4 按字符缓冲写入 BufferedWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void bufferedWriteTest(intcount, String str) {

File f= new File("f:test3.txt");

OutputStreamWriter writer= null;

BufferedWriter bw= null;try{

OutputStream os= newFileOutputStream(f);

writer= newOutputStreamWriter(os);

bw= newBufferedWriter(writer);for (int i = 0; i < count; i++) {

bw.write(str);

}

bw.flush();if(f.exists()){

f.delete();

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***5 按字符缓冲写入 BufferedWriter and BufferedOutputStream

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void bufferedWriteAndBufferedOutputStreamTest(intcount, String str) {

File f= new File("f:test4.txt");

BufferedOutputStream bos=null;

OutputStreamWriter writer= null;

BufferedWriter bw= null;try{

OutputStream os= newFileOutputStream(f);

bos=newBufferedOutputStream(os);

writer= newOutputStreamWriter(bos);

bw= newBufferedWriter(writer);for (int i = 0; i < count; i++) {

bw.write(str);

}

bw.flush();if(f.exists()){

f.delete();

System.out.println("delete---");

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}/***6 按字符缓冲写入 BufferedWriter and FileWriter

*

*@paramcount 写入循环次数

*@paramstr 写入字符串*/

public void bufferedWriteAndFileWriterTest(intcount, String str) {

File f= new File("f:test5.txt");

FileWriter fw=null;

BufferedWriter bw= null;try{

fw=newFileWriter(f);

bw= newBufferedWriter(fw);for (int i = 0; i < count; i++) {

bw.write(str);

}

bw.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bw.close();if(f.exists()){

f.delete();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐