首页 > 解决方案 > 访客详细问题 java 中的 IO 文件处理

问题描述

VIP集团公司推出全新购物中心“乐乐”。为宣传商场,他们联系了著名的商业活动组织者“6th Event”,组织了一场幸运抽奖活动。主办方必须在促销期间收集所有参观者的姓名、电话和电子邮件ID,并将其提供给公司。

组织者需要一个自动化应用程序,并希望将记录存储在一个名为“visitors.txt”的文本文件中。

记录应存储在以下结构中

Name1,phonenumber1,emailId1;Name2,phonenumber2,emailId2;

在一个记录中,每个属性应该用逗号(,)分隔,记录应该用分号(;)分隔。

创建一个 Java 应用程序,它有两个名为 Main.java 和 FileManager.java 的类

在 FileManager 类中实现以下方法[给出方法框架]

static public File createFile() - 这个方法应该创建文件并返回它。

static public void writeFile(File f, String record) - 该方法中,第一个参数是要添加记录的文件引用,第二个参数是一条记录,该记录应附加在文件中。[记录应按照给定的格式]

static public String[] readFile(File f) - 此方法接受要读取的文件,返回文件中的所有记录。

[注意:不要修改给定方法的签名]

在 Main 类中使用以下 Input 和 Output 语句,并从 FileManager 类调用所需的方法来操作文件。

输入名字

约翰

输入电话号码

1234567

输入电子邮件

johnpeter@abc.com

是否要输入另一条记录(是/否)

是的

输入名字

优雅

输入电话号码

98765412

输入电子邮件

Gracepaul@xyz.com

是否要输入另一条记录(是/否)

是否要显示所有记录(是/否)

是的

约翰,1234567,johnpeter@abc.com

格蕾丝,98765412,gracepaul@xyz.com

标签: javaio

解决方案


    FileManager class

    //import necessary packages
import java.io.*;
import java.util.*;
 @SuppressWarnings("unchecked")//Do not delete this line
 public class FileManager 
 {

    static public File createFile()
    {
        File file =new File("visitors.txt");
try{      file.createNewFile();}
catch (IOException e)   
{  
e.printStackTrace();    //prints exception if any  
}      

    return file;
    }
   //change the return type as per the requirement    
    static public void writeFile(File f, String record)
    {    try { 


            BufferedWriter out = new BufferedWriter( 
                   new FileWriter(f.getName(), true)); 
            out.write(record+";"); 
            out.close(); 
        } 
        catch (IOException e) { 
            System.out.println("exception occoured" + e); 
        } 

    } 
    static public String[] readFile(File f)
    {

        List<String> tokens = new ArrayList<String>();
        try{
      File myObj = new File(f.getName());
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {

        // String [] arr= myReader.nextLine().split(";");
        // tokens = Arrays.asList(arr);
        tokens.add(myReader.nextLine());

      }

      myReader.close();
      }
       catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
    String[] tokenArray = tokens.toArray(new String[0]);
    //=tokenArray.split(";");
    return tokenArray;
    }
 }


    Main class


    import java.util.*;
    import java.io.FileNotFoundException;
    //import necessary packages
    import java.io.File;
     @SuppressWarnings("unchecked")//Do not delete this line
    public class Main
    {
        public void abcd(){
            Scanner in = new Scanner(System.in);
            System.out.println("Enter Name");
            String name=in.next();
            System.out.println("Enter Phone Number");
            long phone=in.nextLong();
            System.out.println("Enter Email");
            String id= in.next();
            FileManager f= new FileManager();
            File x =f.createFile();
            f.writeFile(x,name+","+phone+","+id);
            System.out.println("Do you want to enter another record(yes/no)");
            String choice=in.next();
            if(choice.equals("yes")){
                abcd();
            }
            if(choice.equals("no"))
           {String []q=f.readFile(x);
           String pl[]=q[0].split(";");
            for(int i=0;i<pl.length;i++)
            {
                System.out.println(pl[i]);
                   }

                    System.exit(0);
            }

        }

        public static void main(String[] args) 
        {
            Main asd=new Main();
            asd.abcd();



        }

    }

This program gives me desired output but not able to run all test cases.
Getting error could not append multiple files. Dont know is this.But it works perfectly on compiler. And you should at least try to code rather then simply asking someone to code.

推荐阅读