我想一般的server/client程序加上queue代码让检查’quit‘
就对出就行了
就对出就行了
解决方案 »
- String类中equals与hashCode
- 有没有人知道怎么修改jfreechart
- “未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出main(argv)”错误
- 大家帮我看看这个题目
- Document 的实现类是什么?
- 请教一个关于流的问题 急啊~~~~
- 在 Win98 机子上输入 \\192.168.1.2\C$ 为什么不能访问
- System.out.println("a".getBytes("unicode").length)为什么打印为4?
- 如何进行线程管理!
- 如何把一个字符串中所有指定的字符删除?
- 在java JFrame里面动态改变菜单怎么要手工调整一下窗体大小才能正确显示呢?
- 初学者的问题.
{
public static void main(String args[])
{
String temp;
File sourceFile,targetFile;
BufferedReader source;
BufferedWriter target;
try
{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader bufin = new BufferedReader(stdin);
System.out.print("请输入来源文件路径: ");
sourceFile = new File(bufin.readLine());
source = new BufferedReader(new FileReader(sourceFile));
System.out.print("请输入目的文件路径: ");
targetFile = new File(bufin.readLine());
target = new BufferedWriter(new FileWriter(targetFile));
System.out.print("确定要复制?(y/n) ");
if((bufin.readLine()).equals("y"))
{
while((temp=source.readLine()) != null)
{
target.write(temp);
target.newLine();
target.flush();
}
System.out.println("复制文件完成!!!");
}
else
{
System.out.println("复制文件失败!!!");
return;
}
stdin.close();
bufin.close();
}
catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
}
email: [email protected]
public copy(String s,String s1){
from_f=new File(s);
to_f=new File(s1);
}
public void copy_file(){
try{
System.out.println("开始拷贝文件......");
FileInputStream f_in=new FileInputStream(from_f);
FileOutputStream f_out=new FileOutputStream(to_f);
int p=0;
while(true){
byte b[]=new byte[1024];
p=f_in.read(b);
if(p<0)
break;
f_out.write(b,0,p);
}
System.out.println("拷贝文件结束");
}catch(Exception e){
System.out.println("拷贝文件错误!");
System.out.println("错误原因:"+e.toString());
}
}
public static void main(String[] args){
if(args.length<2)
copy.show();
(new copy(args[0],args[1])).copy_file();
while(true)
try{
byte b[]=new byte[50];
int p=System.in.read(b);
if((p-2)>=0)
p=p-2;//去掉回车
String command=new String(b,0,p);
if(command.equals("quit")){
System.out.println("回头见!");
System.exit(0);
}
}catch(Exception e){}
}
public static void show(){
System.out.println("命令格式如下:");
System.out.println("java copy 源文件名 目标文件名");
System.exit(0);
}
}执行命令:
javac copy.java
java copy copy.java bak.java祝你好运!
public static void main(String[] args){
String str="";
System.out.println("======================File I/O Testing=========================");
System.out.println("--input 'quit' to exit--");
//screen input
String sFile="";
String dFile="";
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
//file io
FileInputStream fin=null;
FileOutputStream fout=null;
File f=null;
//get the source file name from screen input and judge the validation
while(!isQuit(str)){
System.out.print("The source file: ");
try{
str=br.readLine();
}catch(IOException x){
}
//System.out.print("debug: " + str);
if(isQuit(str)){
System.exit(0);
}else{
//test wether the file exsiting
try{
f=new File(str);
if(!(f.exists() && f.isFile())){
System.out.println("!!!Error: the file you defined does not exist, try it again...");
continue;
}
sFile=str;
fin=new FileInputStream(str);
}catch(FileNotFoundException x){
}
str="";
break;
}
}
//get the destination file name from screen and judge the validation
while(!isQuit(str)){
System.out.print("The destination file: ");
try{
str=br.readLine();
}catch(IOException x){
}
if(isQuit(str)){
//remember to close the io!
if(fin!=null){
try{
fin.close();
}catch(IOException x){
}
}
System.exit(0);
}else{
//test wether the file exsiting
boolean isContinued=false;
//boolean isBreak=false;
f=new File(str);
if(f.exists() & f.isFile()){
while(true){
System.out.print("The file has been existed, do you want to override it?(y-YES,n-NO,q-Quit):");
String tempStr="";
try{
tempStr=br.readLine();
}catch(IOException x){
}
if(tempStr.equalsIgnoreCase("y")){
break;
}else if(tempStr.equalsIgnoreCase("n")){
System.out.println("Please define another destination file again...");
isContinued=true;
break;
}else if(tempStr.equalsIgnoreCase("q")){
//remember to close the io!
if(fin!=null){
try{
fin.close();
}catch(IOException x){
}
}
System.exit(0);
}else{
continue;
}
}
}
if(isContinued)
continue;
dFile=str;
if(sFile.trim().equals(dFile.trim())){
System.out.println("Invalid operation, it is not allowable to export a file to himself...");
System.out.println("Please redefine another destination file...");
continue;
}
try{
fout=new FileOutputStream(str);
}catch(FileNotFoundException x){
System.out.println("!!!Error: the output file can not be found, try it again...");
continue;
}
break;
}
}
BufferedInputStream bIn=null;
BufferedOutputStream bOut=null;
System.out.println("Begin to export file...");
try{
//read source file
bIn=new BufferedInputStream(fin);
int bNum=bIn.available();
byte[] bt=new byte[bNum];
bIn.read(bt,0,bNum);
//write to destination file
bOut=new BufferedOutputStream(fout);
bOut.write(bt,0,bNum);
bOut.flush();
bIn.close();
bOut.close();
System.out.println("Success to export file from '" + sFile + "' to '" + dFile + "'.");
}catch(IOException x){
System.out.println("Fail to read/write file...");
x.printStackTrace();
}
}
public static boolean isQuit(String str){
return str.equalsIgnoreCase("quit");
}}