10-1编写一个程序,将一个电话号码写入文件中。publicclassTest1{publicstaticvoidmain(String[]args){intb;Filefile=newFile(C:\\,phone.txt);bytebytes[]=newbyte[10];System.out.println(请输入八位电话号码:);try{if(!file.exists())//判断文件是否存在file.createNewFile();//把从键盘输入的字符存入bytes里b=System.in.read(bytes);//创建文件输出流FileOutputStreamfos=newFileOutputStream(file,true);fos.write(bytes,0,b);//把bytes写入到指定文件中fos.close();//关闭输出流}catch(IOExceptione){e.printStackTrace();}}}10-2实现文件的复制。publicclassTest1{publicstaticvoidmain(String[]args){Filefilein=newFile(C:\\,phone.txt);Filefileout=newFile(C:\\,phone-1.txt);FileInputStreamfis;try{if(!filein.exists())//如果文件不存在filein.createNewFile();//创建新文件if(!fileout.exists())//如果文件不存在fileout.createNewFile();//创建新文件fis=newFileInputStream(filein);FileOutputStreamfos=newFileOutputStream(fileout,true);InputStreamReaderin=newInputStreamReader(fis);OutputStreamWriterout=newOutputStreamWriter(fos);intis;while((is=in.read())!=-1){out.write(is);}in.close();out.close();}catch(IOExceptione){e.printStackTrace();}}}10-3使用RandomAccessFile流将一个文本文件倒置读出。publicclassTest1{publicstaticvoidmain(String[]args){intbytes[]={1,2,3,4,5};try{//创建RandomAccessFile类的对象RandomAccessFileraf=newRandomAccessFile(C:\\Example5.txt,rw);for(inti=0;ibytes.length;i++){raf.writeInt(bytes[i]);}for(inti=bytes.length-1;i=0;i--){raf.seek(i*4);//int型数据占4个字节System.out.println(raf.readInt());}raf.close();}catch(Exceptione){e.printStackTrace();}}}10-4使用Java的输入输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。publicclassTest1{publicstaticvoidmain(String[]args)throwsException{appendLineNumber(newFile(C:\\a.txt),newFile(C:\\b.txt));}publicstaticvoidappendLineNumber(Filefrom,Fileto)throwsException{BufferedReaderin=newBufferedReader(newFileReader(from));StringBuildersb=newStringBuilder();Stringt;intlineNumber=1;while((t=in.readLine())!=null){sb.append(lineNumber+、);sb.append(t);sb.append(System.getProperty(line.separator));lineNumber++;}in.close();BufferedWriterout=newBufferedWriter(newFileWriter(to));out.write(sb.toString());out.close();}}