JAVA 如何向控制台输出一个"Y"或"N",并继续执行

cc850107 2008-06-10 08:42:31
这是原来的代码,功能是拷贝一个文件到另一个文件,假如目标文件存在,询问是否覆盖,用户输(Y/N),再执行.我现在想程序自动的输出一个(Y/N),请问如何实现,我试了加(System.out.println("Y\n"); System.out.flush();)不好使.
请问如何实现?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileCopy
{
public static void main(String[] args)
{
try
{
copy("fromFile.txt", "toFile.txt");
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}

public static void copy(String fromFileName, String toFileName)
throws IOException
{
File fromFile = new File(fromFileName);
File toFile = new File(toFileName);

if (!fromFile.exists())
throw new IOException("FileCopy: " + "no such source file: "
+ fromFileName);
if (!fromFile.isFile())
throw new IOException("FileCopy: " + "can't copy directory: "
+ fromFileName);
if (!fromFile.canRead())
throw new IOException("FileCopy: " + "source file is unreadable: "
+ fromFileName);

if (toFile.isDirectory())
toFile = new File(toFile, fromFile.getName());

if (toFile.exists())
{
if (!toFile.canWrite())
throw new IOException("FileCopy: "
+ "destination file is unwriteable: " + toFileName);
System.out.print("Overwrite existing file " + toFile.getName()
+ "? (Y/N): ");
System.out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
//这附近我想自动的输出Y或N,如何实现?
String response = in.readLine();
if (!response.equals("Y") && !response.equals("y"))
throw new IOException("FileCopy: "
+ "existing file was not overwritten.");
}
else
{
String parent = toFile.getParent();
if (parent == null)
parent = System.getProperty("user.dir");
File dir = new File(parent);
if (!dir.exists())
throw new IOException("FileCopy: "
+ "destination directory doesn't exist: " + parent);
if (dir.isFile())
throw new IOException("FileCopy: "
+ "destination is not a directory: " + parent);
if (!dir.canWrite())
throw new IOException("FileCopy: "
+ "destination directory is unwriteable: " + parent);
}

FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;

while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
} finally {
if (from != null)
try {
from.close();
} catch (IOException e) {
;
}
if (to != null)
try {
to.close();
} catch (IOException e) {
;
}
}
}
}
...全文
1282 33 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
wensheng_zh2007 2008-06-15
  • 打赏
  • 举报
回复
pass
l_wenb 2008-06-14
  • 打赏
  • 举报
回复
学习下
帮顶!
chjl2020 2008-06-14
  • 打赏
  • 举报
回复
不懂,回帖纯粹是为了罐水..
galant2008 2008-06-14
  • 打赏
  • 举报
回复
in.readLine();
這個是要鍵盤響應的,另想辦法吧。
zeng_wh 2008-06-14
  • 打赏
  • 举报
回复
学习下
帮顶!
cc850107 2008-06-14
  • 打赏
  • 举报
回复
谢谢大家帮顶,我看看还有多少分给大家加一下啊。
evoloyeu 2008-06-14
  • 打赏
  • 举报
回复
路过
jason_kou 2008-06-14
  • 打赏
  • 举报
回复
很明白楼主的意思,不过找了半天也没找到解决办法,郁闷
楼主要实现的就类似GUI里面的事件监听,等待高手。。
ArthurCX 2008-06-14
  • 打赏
  • 举报
回复
额···关于这个问题···如果逻辑上必须要求执行y以后的代码,那你就直接跳到y里面去执行啊,干嘛非得让它提示Y/N,然后自动输入Y,直接Y不就行了吗??
还是不大懂你的意思·····
cc850107 2008-06-14
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 galant2008 的回复:]
in.readLine();
這個是要鍵盤響應的,另想辦法吧。
[/Quote]
我不指这个函数,那个只是想模拟下控制台下的步骤.
难道如果控制台输出了那个选择Y/N 的命令,不能从程序中输一个Y并使它执行下去?只能在程序中输一个Y,然后手动的输一个回车??
hnjd314053754 2008-06-14
  • 打赏
  • 举报
回复
关注一下
Sou2012 2008-06-13
  • 打赏
  • 举报
回复
帮顶..路过
zj6799904 2008-06-13
  • 打赏
  • 举报
回复
public class Test1{

public static void main(String args[]) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入(y/n)");
String i = in.readLine();
if(i.equals("y"))
System.out.println(i);
}
}

请输入(y/n)
y //输入的Y
y //if判断 之后输出的y

是这个效果吗?

wang_qing_2008 2008-06-13
  • 打赏
  • 举报
回复
学习了
hemaily 2008-06-13
  • 打赏
  • 举报
回复
哎 整个贴下开 楼主看看
------------------------------------------------------------

package com.css.cfets.common.util;

import java.security.*;
import java.io.*;

/**
* CryptTool 封装了一些加密工具方法, 包括 3DES, MD5 等.
*
* @author wang shi ping
* @version 1.0
* 2008-05-12
*/
public class pwdTool {

public pwdTool() {
}



/**
* BASE64 编码.
*
* @param src String inputed string
* @return String returned string
*/
public static String base64Encode(String src) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

return encoder.encode(src.getBytes());
}

/**
* BASE64 编码(byte[]).
*
* @param src byte[] inputed string
* @return String returned string
*/
public static String base64Encode(byte[] src) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

return encoder.encode(src);
}

/**
* BASE64 解码.
*
* @param src String inputed string
* @return String returned string
*/
public static String base64Decode(String src) {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

try {
return new String(decoder.decodeBuffer(src));
}
catch (Exception ex) {
return null;
}

}

/**
* BASE64 解码(to byte[]).
*
* @param src String inputed string
* @return String returned string
*/
public static byte[] base64DecodeToBytes(String src) {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

try {
return decoder.decodeBuffer(src);
}
catch (Exception ex) {
return null;
}

}


/**
* 对给定字符进行 URL 编码.
*
* @param src String
* @return String
*/
/*
public static String urlEncode(String src) {
try {
src = java.net.URLEncoder.encode(src, "GB2312");

return src;
}
catch (Exception ex) {
ex.printStackTrace();
}

return src;
}
*/
/**
* 功能描述:对给定字符进行 URL 解码
* @param value 解码前的字符串
* @return String 解码后的字符串
*/
/*
public String urlDecode(String value) {
try {
return java.net.URLDecoder.decode(value, "GB2312");
}
catch (Exception ex) {
ex.printStackTrace();
}

return value;
}
*/

public static void main(String[] args) {
System.out.println("请选择\n======");
System.out.println("-->按( 1 )加密");
System.out.println("-->按( 2 )解密");
System.out.println("-->按( 3 )退出\n");
String way="";
boolean flag=true;
String words="";
try{
while(flag){
BufferedReader input = new BufferedReader(new InputStreamReader(System.
in));
way = input.readLine();
if (way.equals("1")){
System.out.println("输入你需要加密的字符串");
words = input.readLine();
System.out.println("加密后: " + base64Encode(words));
flag=false;
}
else if (way.equals("2")) {
System.out.println("输入你需要解密的字符串");
words=input.readLine();
System.out.println("解密后: " + base64Decode(words));
flag=false;
}else if(way.equals("3")){
flag=false;
}
else {
System.out.println("请正确输入");
flag=true;
}
}
}catch(Exception e){
e.printStackTrace();
}


}

}


junny9985 2008-06-13
  • 打赏
  • 举报
回复
晕了 理解错了楼主的意思了
junny9985 2008-06-13
  • 打赏
  • 举报
回复

//
BufferedRead in = new BufferedRead(new InputStream(System.in));
String line = in.readLine();
//
if(line.equls("y") || line.equals("Y"))
{
System.out.println("U press Y");
}
else ifline.equls("y") || line.equals("N"))
{
System.out.println("U press N");
}
awusoft 2008-06-12
  • 打赏
  • 举报
回复
System.out.print("Overwrite existing file " + toFile.getName()
+ "? (Y/N): \nY\n");
//下边的代码都不需要了啊.



System.out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
//这附近我想自动的输出Y或N,如何实现?
String response = in.readLine();
if (!response.equals("Y") && !response.equals("y"))
throw new IOException("FileCopy: "
+ "existing file was not overwritten.");
awusoft 2008-06-12
  • 打赏
  • 举报
回复
晕...你不要等待输入啊
awusoft 2008-06-12
  • 打赏
  • 举报
回复
System.out.println("Y\\N");
System.out.flush();

System.out.println("Y/N");
加载更多回复(12)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧