| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 633 人关注过本帖
标题:又一个输入输出问题,有看不懂的地方.
只看楼主 加入收藏
songdeguang
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2006-4-5
收藏
 问题点数:0 回复次数:4 
又一个输入输出问题,有看不懂的地方.

程序采用命令行参数拷贝一个文件,然后输出该文件的内容.
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 
2006-04-17 11:35
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分: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);
//作用??就是什么意思??构造两个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来说,没有必要一开始就看这样的


可惜不是你,陪我到最后
2006-04-17 16:37
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 
有关io 包  一般书都是在最后介绍的 前面基础要掌握好

------Java 爱好者,论坛小混混,学习中------
2006-04-18 13:02
songdeguang
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2006-4-5
收藏
得分:0 
看来学习JAVA不能越级打怪啊  哈哈  谢谢了

STUDY AND STUDY—JAVA
2006-04-18 17:28
164726003
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2005-7-12
收藏
得分:0 
.....玩游戏呢

坚持!!!
2006-04-18 21:14
快速回复:又一个输入输出问题,有看不懂的地方.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014331 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved