当前位置: 代码迷 >> Java相关 >> 又一个输入输出问题,有看不懂的地方.
  详细解决方案

又一个输入输出问题,有看不懂的地方.

热度:73   发布时间:2006-04-17 11:35:00.0
又一个输入输出问题,有看不懂的地方.

程序采用命令行参数拷贝一个文件,然后输出该文件的内容.
import java.io.*;
public class copyAndShow{
// 文件拷贝方法
void copy(String fromFile, String toFile)
throws IOException{

File src=new File(fromFile);
File dst=new File(toFile);
//作用??就是什么意思??
if(!src.exists( )){
System.out.println(fromFile+" does not exist!");
System.exit(1);
}
if(!src.isFile( )){
System.out.println(fromFile+" is not a file!");
System.exit(1);
}
if(!src.canRead( )){
System.out.println(fromFile+" is unreadable!");
System.exit(1);
}
if(dst.exists( )){
if(!dst.canWrite( )){
System.out.println(toFile+" is unwriteable!");
System.exit(1);
}
}

// 执行拷贝操作
FileInputStream fin=null; // 采用文件输入流
FileOutputStream fout=null;
// 什么意思呢?
try{ fin=new FileInputStream(src);
fout=new FileOutputStream(dst); //又是什么意思?
byte buffer[]=new byte[4096];
int bytesRead; // 从缓冲区读入的字节数
while((bytesRead=fin.read(buffer))!=-1)
fout.write(buffer,0,bytesRead);
}finally{
if(fin!=null) //为什么这时候是异常关闭呢? fin!=null 这句话代表什么意思?
try{ fin.close();
fout.close();
}catch(IOException e){
System.out.println("关闭文件异常");
}
}
}
// 显示文件内容方法
void showContents(String fileName)throws IOException{
File f=new File(fileName);
RandomAccessFile fin=new
RandomAccessFile(f,"rw");
System.out.println("File length: "+fin.length( ));
// 文件长度
System.out.println("position:"+fin.getFilePointer( ));

// 按行显示文件内容
while(fin.getFilePointer( )<fin.length( ))
System.out.println(fin.readLine( ));
fin.close( );
}
public static void main(String args[ ]){
if(args.length!=2){
System.out.println("Usage: java copyAndShow
<srcFileName dstFileName>");
System.exit(1);
}
try{ copyAndShow obj=new copyAndShow ();
obj.copy(args[0],args[1]);
obj.showContents(args[1]);
}catch(IOException e){
System.out.println(e.getMessage( ));
}
}
}

搜索更多相关的解决方案: File  输出  String  java  

----------------解决方案--------------------------------------------------------

程序采用命令行参数拷贝一个文件,然后输出该文件的内容.
import java.io.*;
public class copyAndShow{
// 文件拷贝方法
void copy(String fromFile, String toFile)
throws IOException{

File src=new File(fromFile);
File dst=new File(toFile);
//作用??就是什么意思??构造两个File对象啊
if(!src.exists( )){
System.out.println(fromFile+" does not exist!");
System.exit(1);
}
if(!src.isFile( )){
System.out.println(fromFile+" is not a file!");
System.exit(1);
}
if(!src.canRead( )){
System.out.println(fromFile+" is unreadable!");
System.exit(1);
}
if(dst.exists( )){
if(!dst.canWrite( )){
System.out.println(toFile+" is unwriteable!");
System.exit(1);
}
}

// 执行拷贝操作
FileInputStream fin=null; // 采用文件输入流
FileOutputStream fout=null;
// 什么意思呢?先把它赋成null值。写不写差不多
try{ fin=new FileInputStream(src);
fout=new FileOutputStream(dst); //又是什么意思?一节节构造一个输出流
byte buffer[]=new byte[4096];
int bytesRead; // 从缓冲区读入的字节数
while((bytesRead=fin.read(buffer))!=-1)
fout.write(buffer,0,bytesRead);
}finally{
if(fin!=null) //为什么这时候是异常关闭呢? fin!=null 这句话代表什么意思?如果不是空,就关闭!!!
try{ fin.close();
fout.close();
}catch(IOException e){
System.out.println("关闭文件异常");
}
}
}
// 显示文件内容方法
void showContents(String fileName)throws IOException{
File f=new File(fileName);
RandomAccessFile fin=new
RandomAccessFile(f,"rw");
System.out.println("File length: "+fin.length( ));
// 文件长度
System.out.println("position:"+fin.getFilePointer( ));

// 按行显示文件内容
while(fin.getFilePointer( )<fin.length( ))
System.out.println(fin.readLine( ));
fin.close( );
}
public static void main(String args[ ]){
if(args.length!=2){
System.out.println("Usage: java copyAndShow
<srcFileName dstFileName>");
System.exit(1);
}
try{ copyAndShow obj=new copyAndShow ();
obj.copy(args[0],args[1]);
obj.showContents(args[1]);
}catch(IOException e){
System.out.println(e.getMessage( ));
}
}
}

建议楼主先学一些JAVA基础的东西,输入输入以后再慢慢学
还有,凡事不能急,要一步一步来
然后再从最基本的IO来说,没有必要一开始就看这样的


----------------解决方案--------------------------------------------------------
有关io 包  一般书都是在最后介绍的 前面基础要掌握好
----------------解决方案--------------------------------------------------------
看来学习JAVA不能越级打怪啊  哈哈  谢谢了
----------------解决方案--------------------------------------------------------
.....玩游戏呢

----------------解决方案--------------------------------------------------------
  相关解决方案