如何在打印上一个文件后重新加载java中的其他文件
我下面的代码正在编译,但我有一个逻辑错误。好的,在我输入了我想要读取的数据后,我选择在屏幕上打印这些数据,我可以选择加载一个不同的文件。但是,当我输入要加载的新文件的名称时,它不会加载该文件以及我为该特定情况指定的错误消息。我想我必须在每次写入或类似的事情之后刷新流缓冲区。所以,如果有人能指出为什么会发生这种情况,我将不胜感激 导入java.util.Scanner; 导入java.io.*如何在打印上一个文件后重新加载java中的其他文件,java,Java,我下面的代码正在编译,但我有一个逻辑错误。好的,在我输入了我想要读取的数据后,我选择在屏幕上打印这些数据,我可以选择加载一个不同的文件。但是,当我输入要加载的新文件的名称时,它不会加载该文件以及我为该特定情况指定的错误消息。我想我必须在每次写入或类似的事情之后刷新流缓冲区。所以,如果有人能指出为什么会发生这种情况,我将不胜感激 导入java.util.Scanner; 导入java.io.* public class Driver { private static int numberO
public class Driver {
private static int numberOfCustomer = 0;
private static Customer[] customerList = new Customer[10];
private static void readInCustomer(String file){
FileReader freader;
BufferedReader inputFile;
try{
freader = new FileReader(file);
inputFile = new BufferedReader(freader);
String strLine;
while ((strLine = inputFile.readLine()) != null) {
customerList[numberOfCustomer] = new Customer();
customerList[numberOfCustomer].ID = strLine;
customerList[numberOfCustomer].name = inputFile.readLine();
customerList[numberOfCustomer].address = inputFile.readLine();
customerList[numberOfCustomer].phone = inputFile.readLine();
numberOfCustomer++;
}
inputFile.close();
}catch(Exception e){
System.out.println("Could not find file "+file+" System will now exit");
System.exit(1);
}
return;
}
private static void printCustomer(Customer customer){
System.out.println("The Customer Data corresponding to Customer Number " + customer.ID + " is:");
System.out.println("Name:\t\t\t"+customer.name);
System.out.println("Address:\t\t"+customer.address);
System.out.println("Telephone:\t\t"+customer.phone);
System.out.println();
return;
}
private static void printAll(){
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("All customers from data file "+numberOfCustomer);
System.out.println(" Here they are!!! ");
for(int i=0; i<numberOfCustomer; i++){
if(customerList[i] != null){
System.out.println("The Customer Data corresponding to Customer Number " + customerList[i].ID + " is:");
System.out.println("Name:\t\t\t"+customerList[i].name);
System.out.println("Address:\t\t"+customerList[i].address);
System.out.println("Telephone:\t\t"+customerList[i].phone);
}
}
if(!hasID){
System.out.println("");
}
System.out.println("Would you like to go to the menu? (Y or N):");
String input = keyboard.nextLine();
char repeat = input.charAt(0);
if(repeat == 'Y' || repeat == 'y'){Menu();}
return;
}
private static void Menu(){
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:");
System.out.println("A. SEARCH for a customer by ID number");
System.out.println("B. DISPLAY the entire Customer List");
System.out.println("C. RE-LOAD DATA from a different data file");
System.out.println("D. QUIT:");
String choice = keyboard.nextLine();
char repeat = choice.charAt(0);
if(repeat == 'A' || repeat == 'a'){Scostomer();}
if(repeat == 'B' || repeat == 'b'){printAll();}
if(repeat == 'C' || repeat == 'c'){mainn();}
return;
}
public static void Scostomer(){
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("Type in the Id you are search for");
String customerID = keyboard.nextLine();
for(int i=0; i<numberOfCustomer; i++){
if((customerList[i]!=null) && (customerID.equals(customerList[i].ID))){
hasID = true;
printCustomer(customerList[i]);
i=customerList.length;
}
}
if(!hasID){
System.out.println("Sorry, customer not found.");
}
System.out.println("Would you like to search for another custnomer? (Y or N):");
String input = keyboard.nextLine();
char repeat = input.charAt(0);
if(repeat == 'Y' || repeat == 'y'){Scostomer();}
if(repeat == 'N' || repeat == 'n'){Menu();}
return;
}
public static void main(String arg[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the fileName that contains the data of your customers: ");
readInCustomer(keyboard.nextLine());
Menu();
return;
}
public static void mainn(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the fileName that contains the data of your customers: ");
readInCustomer(keyboard.nextLine());
Menu();
return;
}
}
适用于我加载文件,键入c加载新文件,键入b显示所有文件。我建议文件名有一些简单的错误或愚蠢的地方,所以您应该记录异常详细信息,而不是在catch块中忽略它们
} catch(Exception e) {
e.printStackTrace(); // should tell you what's wrong!
System.out.println("Could not find file "+file+" System will now exit");
System.exit(1);
}
冲洗在这里不相关,因为你是在阅读,而不是写作。你也可以整理一下整个菜单/输入循环,虽然有点古怪,但它确实有效!。例如,您的main和mainn方法是相同的-您是否考虑过将其抽象为单独的方法
您可以使用一个列表,而不必担心要阅读多少客户,而不是将自己限制为10个客户。我只是将您的代码更改为使用列表而不是数组:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Driver {
private static int numberOfCustomer = 0;
// List instead of array!
private static List<Customer> customerList = new ArrayList<Customer>();
private static void readInCustomer(String file) {
FileReader freader;
BufferedReader inputFile;
try {
freader = new FileReader(file);
inputFile = new BufferedReader(freader);
String strLine;
while ((strLine = inputFile.readLine()) != null) {
Customer customer = new Customer();
customer.ID = strLine;
customer.name = inputFile.readLine();
customer.address = inputFile.readLine();
customer.phone = inputFile.readLine();
customerList.add(customer); // add to the List!
}
inputFile.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Could not find file " + file
+ " System will now exit");
System.exit(1);
}
return;
}
private static void printCustomer(Customer customer) {
System.out
.println("The Customer Data corresponding to Customer Number "
+ customer.ID + " is:");
System.out.println("Name:\t\t\t" + customer.name);
System.out.println("Address:\t\t" + customer.address);
System.out.println("Telephone:\t\t" + customer.phone);
System.out.println();
return;
}
private static void printAll() {
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("All customers from data file " + numberOfCustomer);
System.out.println(" Here they are!!! ");
for (Customer customer : customerList) {
System.out
.println("The Customer Data corresponding to Customer Number "
+ customer.ID + " is:");
System.out.println("Name:\t\t\t" + customer.name);
System.out.println("Address:\t\t" + customer.address);
System.out.println("Telephone:\t\t" + customer.phone);
}
if (!hasID) {
System.out.println("");
}
System.out.println("Would you like to go to the menu? (Y or N):");
String input = keyboard.nextLine();
char repeat = input.charAt(0);
if (repeat == 'Y' || repeat == 'y') {
Menu();
}
return;
}
private static void Menu() {
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:");
System.out.println("A. SEARCH for a customer by ID number");
System.out.println("B. DISPLAY the entire Customer List");
System.out.println("C. RE-LOAD DATA from a different data file");
System.out.println("D. QUIT:");
String choice = keyboard.nextLine();
char repeat = choice.charAt(0);
if (repeat == 'A' || repeat == 'a') {
Scostomer();
}
if (repeat == 'B' || repeat == 'b') {
printAll();
}
if (repeat == 'C' || repeat == 'c') {
mainn();
}
return;
}
public static void Scostomer() {
boolean hasID = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("Type in the Id you are search for");
String customerID = keyboard.nextLine();
// iterate over the List!
for (Customer customer : customerList) {
if (customerID.equals(customer.ID)) {
hasID = true;
printCustomer(customer);
break;
}
}
if (!hasID) {
System.out.println("Sorry, customer not found.");
}
System.out
.println("Would you like to search for another custnomer? (Y or N):");
String input = keyboard.nextLine();
char repeat = input.charAt(0);
if (repeat == 'Y' || repeat == 'y') {
Scostomer();
}
if (repeat == 'N' || repeat == 'n') {
Menu();
}
return;
}
public static void main(String arg[]) {
Scanner keyboard = new Scanner(System.in);
System.out
.println("Enter the fileName that contains the data of your customers: ");
readInCustomer(keyboard.nextLine());
Menu();
return;
}
public static void mainn() {
Scanner keyboard = new Scanner(System.in);
System.out
.println("Enter the fileName that contains the data of your customers: ");
readInCustomer(keyboard.nextLine());
Menu();
return;
}
}
尝试将“e.printStackTrace”放在readInCustomer的catch块中,然后希望您能找到问题,如果没有,请将该跟踪与您的问题放在一起?您的代码非常适合我。检查以确保您拥有正确的文件路径,并且键入的文件正确。问题是,我要加载的第二个txt文件中的字符串较长,因此我必须增加私有静态客户[]customerList=新客户[10];到私有静态客户[]customerList=新客户[30];哇,谢谢你!我见过e.printStackTrace;在Grad之前,它给了我这个错误java. Lang.ArayIdxOutOfFunsExpRe: 10在Reald.Read in CubjelDistabase.java:33,所以我只是把数字从10到30,它应该考虑使用一个集合,即列表,而不是数组,如果你不能保证你总是拥有最多10个客户。通常不赞成打印到System.out并使用e.printStackTrace-建议使用commmons logging、slf4j等日志框架。但是,对于快速和不干净的日志记录,或者对于独立的命令行应用程序来说,它是很好的。那么,你建议我怎么做?啊,那么,使用列表可以消除一些限制啊,好的,谢谢你,我会记得,从现在开始,弗洛姆,你帮了我很多,谢谢