VIP集团公司推出全新购物中心“乐乐”。为了宣传商场,他们联系了著名的商业 Activity 组织者“6th Event”来举办抽奖 Activity 。主办方必须在促销期间收集所有访客的姓名、电话和电子邮件 ID,并将其提供给公司。
组织者需要一个自动化应用程序,并希望将记录存储在名为“visitors.txt”的文本文件中。
记录应存储在以下结构中
姓名1,电话号码1,邮箱ID1;姓名2,电话号码2,邮箱ID2;
在记录中,各个属性应使用逗号(,)分隔,记录应使用分号(;)分隔。
创建一个 Java 应用程序,其中包含两个名为 Main.java 和 FileManager.java 的类
在FileManager类中实现以下方法[给出方法框架]
static public File createFile() – 此方法应该创建文件并返回它。
static public void writeFile(File f, String record) – 在该方法中,第一个参数是要添加记录的文件引用,第二个参数是一条记录,该记录应追加到文件中。 [记录应按照给定的格式]
static public String[] readFile(File f) – 该方法接受要读取的文件,返回文件中的所有记录。
[注意:不要修改给定方法的签名]
在主类中使用以下输入和输出语句,并从 FileManager 类调用所需的方法来操作文件。
输入姓名
约翰
输入电话号码
1234567
输入电子邮件
johnpeter@abc.com
是否要输入另一条记录(是/否)
是的
输入姓名
格蕾丝
输入电话号码
98765412
输入电子邮件
gracepaul@xyz.com
是否要输入另一条记录(是/否)
没有
是否显示所有记录(是/否)
是的
约翰,1234567,johnpeter@abc.com
格蕾丝,98765412,gracepaul@xyz.com
最佳答案
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.
关于java - 访客详情问题 java 中的 IO 文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61857219/