当我运行时,它将转到名为niceJob.txt的文件,数据来自的文件名为file2.txt,其中包含
niceJob.txt 40
20 1 1 5 7 45 1 2 3 4 5 6 7 8 9 77 88 99 23 34 56
当我打开niceJob.txt时,它将显示
X8
我真的对为什么以及如何发生感到困惑。这是代码:
import java.io.*;
import java.util.Scanner;
public class JH1_00668860 {
public static void printToScreen(String filename) {
Scanner scan = null;
try {
FileInputStream fis = new FileInputStream(filename);
scan = new Scanner(fis);
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("printToScreen: can't open: " + filename);
} finally {
if (scan != null)
scan.close();
}
}// end of print
public static void process(String inputFilename) {
String fileoutputname = null;
FileInputStream file = null;
Scanner scan = null;
FileOutputStream outputFilename = null;
FileWriter ps = null;
try {
file = new FileInputStream(inputFilename);
scan = new Scanner(file);
fileoutputname = scan.next();
System.out.println(fileoutputname + " asfasdfasdfasdf");
outputFilename = new FileOutputStream(fileoutputname);
ps = new FileWriter(fileoutputname);
while (scan.hasNextInt()) {
if (scan.nextInt() >= 0) {
// System.out.println(scan.nextInt() + "asfs");
ps.write(scan.nextInt());
ps.flush();
} else {
System.out.println("You have ran out of data or you have a bad value");
}
}
System.out.println("A file was created");
} catch (FileNotFoundException e) {
System.out.println("You ran into an exception :" + e);
} catch (IOException e) {
System.out.println("You ran into an exception :" + e);
} finally {
try {
if (file != null) {
file.close();
}
if (outputFilename != null) {
outputFilename.close();
}
if (ps != null) {
ps.close();
}
// FileInputStream st = new FileInputStream(fileoutputname);
// int contents = st.read();
// while (scan.hasNextInt()) {
// System.out.print(contents);
// }
if (scan != null) {
scan.close();
}
printToScreen(fileoutputname);
} catch (IOException e) {
System.out.println("there was an exception");
}
}
}
public static void main(String args[]) {
process("file2.txt");
}
}
答案 0 :(得分:0)
scan.nextInt()
循环中多次调用while
(两次),并使用第二次调用scan.nextInt()
来写入文件ps.write(scan.nextInt())
;它会跳过每一个备用integer
您将integer
传递给ps.write()
,而传递string
import java.util.Scanner;
import java.io.*;
public class Main {
public static void printToScreen(String filename) {
Scanner scan = null;
try {
FileInputStream fis = new FileInputStream(filename);
scan = new Scanner(fis);
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("printToScreen: can't open: " + filename);
} finally {
if (scan != null)
scan.close();
}
}// end of print
public static void process(String inputFilename) {
String fileoutputname = null;
FileInputStream file = null;
Scanner scan = null;
FileOutputStream outputFilename = null;
FileWriter ps = null;
try {
file = new FileInputStream(inputFilename);
scan = new Scanner(file);
fileoutputname = scan.next();
System.out.println(fileoutputname + " asfasdfasdfasdf");
outputFilename = new FileOutputStream(fileoutputname);
ps = new FileWriter(fileoutputname);
int currentInt = -1;
while (scan.hasNextInt()) {
currentInt = scan.nextInt();
if (currentInt >= 0) {
//System.out.println(currentInt + "asfs");
ps.write(String.valueOf(currentInt));
ps.flush();
} else {
System.out.println("You have ran out of data or you have a bad value");
}
}
System.out.println("A file was created");
} catch (FileNotFoundException e) {
System.out.println("You ran into an exception :" + e);
} catch (IOException e) {
System.out.println("You ran into an exception :" + e);
} finally {
try {
if (file != null) {
file.close();
}
if (outputFilename != null) {
outputFilename.close();
}
if (ps != null) {
ps.close();
}
// FileInputStream st = new FileInputStream(fileoutputname);
// int contents = st.read();
// while (scan.hasNextInt()) {
// System.out.print(contents);
// }
if (scan != null) {
scan.close();
}
printToScreen(fileoutputname);
} catch (IOException e) {
System.out.println("there was an exception");
}
}
}
public static void main(String args[]) {
process("sample.txt");
}
}
输出:
niceJob.txt asfasdfasdfasdf
A file was created
4020115745123456789778899233456