java使用sanner进行i/o和文件重定向
我试图读取输入(从键盘和命令行文件重定向),处理输入信息。并将其输出到一个文件中。我的理解是,使用以下代码,并使用命令行:java使用sanner进行i/o和文件重定向,java,file,printing,io,system.in,Java,File,Printing,Io,System.in,我试图读取输入(从键盘和命令行文件重定向),处理输入信息。并将其输出到一个文件中。我的理解是,使用以下代码,并使用命令行:javaprogramnameoutput.txt我们应该能够将输出打印到文件中。但它不起作用。有人能帮我指出正确的方法吗?非常感谢 public static void main(String[] args) { Scanner sc = new Scanner (System.in); int [] array= new int[100]; w
javaprogramnameoutput.txt
我们应该能够将输出打印到文件中。但它不起作用。有人能帮我指出正确的方法吗?非常感谢
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int [] array= new int[100];
while (sc.hasNextLine()){
String line = sc.nextLine();
.....//do something
}
请尝试下面的代码
import java.io.*;
import java.util.*;
/**
*
* @author Selva
*/
public class Readfile {
public static void main(String[] args) throws FileNotFoundException, IOException{
if (args.length==0)
{
System.out.println("Error: Bad command or filename.");
System.exit(0);
}else {
InputStream in = new FileInputStream(new File(args[0]));
OutputStream out = new FileOutputStream(new File(args[1]));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
import java.io.*;
import java.util.*;
/**
*
* @author Selva
*/
public class Readfile {
public static void main(String[] args) throws FileNotFoundException, IOException{
Scanner s=new Scanner(System.in);
if (args.length==0)
{
System.out.println("Error: Bad command or filename.");
System.exit(0);
}else {
System.out.println("Enter Input FileName");
String a=s.nextLine();
System.out.println("Enter Output FileName");
String b=s.nextLine();
InputStream in = new FileInputStream(new File(a));
OutputStream out = new FileOutputStream(new File(b));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
使用以下命令运行此代码
如果您的文本文件和java代码是相同的文件夹,请运行如下程序
java Readfile c:\input.txt c:\output.txt
java Readfile input.txt output.txt
如果您的文本文件和java代码是不同的文件夹,请运行如下程序
java Readfile c:\input.txt c:\output.txt
如果您使用扫描仪读取输入表单键盘。请尝试以下代码
import java.io.*;
import java.util.*;
/**
*
* @author Selva
*/
public class Readfile {
public static void main(String[] args) throws FileNotFoundException, IOException{
if (args.length==0)
{
System.out.println("Error: Bad command or filename.");
System.exit(0);
}else {
InputStream in = new FileInputStream(new File(args[0]));
OutputStream out = new FileOutputStream(new File(args[1]));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
import java.io.*;
import java.util.*;
/**
*
* @author Selva
*/
public class Readfile {
public static void main(String[] args) throws FileNotFoundException, IOException{
Scanner s=new Scanner(System.in);
if (args.length==0)
{
System.out.println("Error: Bad command or filename.");
System.exit(0);
}else {
System.out.println("Enter Input FileName");
String a=s.nextLine();
System.out.println("Enter Output FileName");
String b=s.nextLine();
InputStream in = new FileInputStream(new File(a));
OutputStream out = new FileOutputStream(new File(b));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
问题出在哪里?控制台或输出文件中没有打印任何内容。谢谢您提供的信息。这很有帮助。但是,有没有办法避免使用FileInputStream?我想输入流只能接受文件作为输入,而不能从键盘输入?请参阅我的答案,从键盘读取文件名。它的工作原理与您预期的一样