A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© xtf 中级黑马   /  2015-6-21 11:17  /  336 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

TestInputStream1():

String strFile = "Data/DataGroup.cpp";

File = new File(strFile);

InputStream in = null;

in = new FileInputStream(file);

//创建合适文件大小的数组,一次性把数据从文件中读出

b1 = new byte[(int)file.length()];

// 读取文件中的内容到b[]数组,如果read()返回读取的字节内容,

// 当内容为空时返回-1,可以以此作为判断文件内容是否读取完毕        

in.read(b1);      

in.close();

textArea.append(new String(b1));



TestInputStream2():

int iSize = 1024;

boolean blSleep = true;

String strFile = "Data/DataGroup.cpp";

File file = new File(strFile);

int iCount = (int)file.length()/iSize;

InputStream in = null;     

in = new FileInputStream(file);        

b2 =new byte[iSize];

int temp = 0;

int no = 0;

while((temp = in.read()) != -1){

                                     no++;

                                     in.read(b2);                        

                                     System.out.println(new String(b2));                                   

                                     if(no < iCount){

                                               in.read(b2);

                                               System.out.println(new String(b2));

                                     }

                                     else{

                                               blSleep = false;

                                               System.out.print((char)temp);

                                     }

                            }

in.close();













执行TestInputStream1(),后:

TestOutputStream(): (读文本文件,然后显示到组件,写到另外一个文件)

// 写文件时的相关变量

String strOut = "*Data/DataGroup_copy.cpp";

File file = new File(strOut);

OutputStream output = null;

output = new FileOutputStream(file);

output.write(b1);

output.close();



TestPrintStream()://读取并输出屏幕上,输出到文件上

String strFile = "Data/DataGroup.cpp";

File file = new File(strFile);

InputStream in = null;     

in = new FileInputStream(file);

//创建合适文件大小的数组,一次性把数据从文件中读出

b1 = new byte[(int)file.length()];              

// 读取文件中的内容到b[]数组,如果read()返回读取的字节内容,

// 当内容为空时返回-1,可以以此作为判断文件内容是否读取完毕        

in.read(b1);      

in.close();

               

// 将数据输出到屏幕

PrintStream printStream = new PrintStream(System.out);

printStream.println(new String(b1));

printStream.flush();    // 彻底完成输出并清空缓冲区

printStream.close();

      

// 将数据输出到文件

File filePrint = new File("Data/print.cpp");

printStream = new PrintStream(filePrint);

printStream.println(new String(b1));

printStream.close();   

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马