代码如下
private String read(){
try{
FileInputStream fis=openFileInput(FILE_NAME);
byte[] buff=new byte[1024];
int hasRead=0;
StringBuilder sb=new StringBuilder("");
while ((hasRead=fis.read(buff))>0){
sb.append(new String(buff,0,hasRead));
}fis.close();
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}return null;
}
private void write(String content){
try
{
FileOutputStream fos=openFileOutput(FILE_NAME,MODE_APPEND);
PrintStream ps=new PrintStream(fos);
ps.println(content);
ps.close();
}catch (Exception E){
E.printStackTrace();
}
}
我只知道定义了一个读取,写入的方法,但是具体里面每一句代码是干嘛的。。不是很清楚,求大神帮忙解释一下,非常感谢
private String read(){
try{
FileInputStream fis=openFileInput(FILE_NAME);//打开文件读取
byte[] buff=new byte[1024];//建立1k的缓冲区
int hasRead=0;
StringBuilder sb=new StringBuilder("");//建立字符串Builder对象,进行字符串连接比String快
while ((hasRead=fis.read(buff))>0){ //读取文件,每次最多读入1k,如果到文件末尾,则会返回-1,所以这里用>0表示有内容,fis.read返回的是实际读取的字节数,比如到最后一点了,可能读不满1k数据
sb.append(new String(buff,0,hasRead)); //读取的内容添加到字符串中
}fis.close(); //操作完文件后,必须关闭
return sb.toString(); //返回读取的文件内容
}catch (Exception e){
e.printStackTrace(); //异常打印
}return null; //如果有异常,返回null
}
private void write(String content){
try
{
FileOutputStream fos=openFileOutput(FILE_NAME,MODE_APPEND); //打开文件写入,MODE_APPEND该模式下创建的文件其他应用无权访问,并且本应用将在原有的内容后面追加内容
PrintStream ps=new PrintStream(fos); //用PrintSteam便于输出字符串,而不仅仅字节流
ps.println(content); //写入内容
ps.close(); //关闭文件
}catch (Exception E){
E.printStackTrace();
}
}
private String read(){ try{ FileInputStream fis=openFileInput(FILE_NAME);//打开文件读取 byte[] buff=new byte[1024];//建立1k的缓冲区 int hasRead=0; StringBuilder sb=new StringBuilder("");//建立字符串Builder对象,进行字符串连接比String快 while ((hasRead=fis.read(buff))>0){ //读取文件,每次最多读入1k,如果到文件末尾,则会返回-1,所以这里用>0表示有内容,fis.read返回的是实际读取的字节数,比如到最后一点了,可能读不满1k数据 sb.append(new String(buff,0,hasRead)); //读取的内容添加到字符串中 }fis.close(); //操作完文件后,必须关闭 return sb.toString(); //返回读取的文件内容 }catch (Exception e){ e.printStackTrace(); //异常打印 }return null; //如果有异常,返回null } private void write(String content){ try { FileOutputStream fos=openFileOutput(FILE_NAME,MODE_APPEND); //打开文件写入,MODE_APPEND该模式下创建的文件其他应用无权访问,并且本应用将在原有的内容后面追加内容 PrintStream ps=new PrintStream(fos); //用PrintSteam便于输出字符串,而不仅仅字节流 ps.println(content); //写入内容 ps.close(); //关闭文件 }catch (Exception E){ E.printStackTrace(); } }
private String read(){ try{ FileInputStream fis=openFileInput(FILE_NAME);//打开文件读取 byte[] buff=new byte[1024];//建立1k的缓冲区 int hasRead=0; StringBuilder sb=new StringBuilder("");//建立字符串Builder对象,进行字符串连接比String快 while ((hasRead=fis.read(buff))>0){ //读取文件,每次最多读入1k,如果到文件末尾,则会返回-1,所以这里用>0表示有内容,fis.read返回的是实际读取的字节数,比如到最后一点了,可能读不满1k数据 sb.append(new String(buff,0,hasRead)); //读取的内容添加到字符串中 }fis.close(); //操作完文件后,必须关闭 return sb.toString(); //返回读取的文件内容 }catch (Exception e){ e.printStackTrace(); //异常打印 }return null; //如果有异常,返回null } private void write(String content){ try { FileOutputStream fos=openFileOutput(FILE_NAME,MODE_APPEND); //打开文件写入,MODE_APPEND该模式下创建的文件其他应用无权访问,并且本应用将在原有的内容后面追加内容 PrintStream ps=new PrintStream(fos); //用PrintSteam便于输出字符串,而不仅仅字节流 ps.println(content); //写入内容 ps.close(); //关闭文件 }catch (Exception E){ E.printStackTrace(); } }