创建和写入文件
Creating & Writing to a File
此程序提示用户输入联系人列表要写入的文件的名称。他们输入姓名、phone 和电子邮件等信息,它会打印到屏幕并写入文件。输入的信息打印到屏幕上,文件被创建,但注释被写入其中。我错过了什么?
import java.util.*;
import java.io.*;
public class ContactInfo
{
public static void main(String[] args) throws IOException
{
try
{
int Command = 0;
String FileName = "";
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.print("Enter the name of the file for the contact list: ");
FileName = input.nextLine();
System.out.println();
File OutPutFile = new File(FileName);
PrintWriter ContactList = new PrintWriter(OutPutFile);
ContactList.format("%15s%12s%20s\n", "Name", "Phone Number", "Email Address" );
ContactList.format("%15s%12s%20s\n", "------------", "------------", "---------------------" );
String First_Name = "";
String Last_Name = "";
String Remove_Last_Name = "";
String Phone_Number = "";
String Email_Address = "";
//Information InformationObject = new Information(String,String,String,String);
TreeMap<String, Information> Entries = new TreeMap<String, Information>();
do
{
System.out.println();
PrintMenu();
System.out.println();
System.out.print("Enter a command ");
Command = input2.nextInt();
System.out.println();
if (Command == 1)
{
System.out.print("Enter the person's first name: ");
First_Name = input.nextLine();
System.out.println();
System.out.print("Enter the person's last name: ");
Last_Name = input.nextLine();
System.out.println();
System.out.print("Enter the person's phone number: ");
Phone_Number = input.nextLine();
System.out.println();
System.out.print("Enter the person's email address: ");
Email_Address = input.nextLine();
System.out.println();
Information InformationObject = new Information(First_Name, Last_Name, Phone_Number, Email_Address);
Entries.put(InformationObject.getLastName(), InformationObject );
}
if (Command == 2)
{
System.out.print("Enter the last name of the person whom you want to delete from the list: ");
Remove_Last_Name = input.nextLine();
System.out.println();
Entries.remove(Remove_Last_Name);
}
if (Command == 3)
{
for (Map.Entry Document : Entries.entrySet() )
{
Information IO = Entries.get( Document.getKey());
System.out.println( IO.getLastName() + ", " + IO.getFirstName() + " " + IO.getPhoneNumber() + " " + IO.getEmail() );
ContactList.format("%15s%12s%20s\n", IO.getFirstName() + IO.getLastName(), IO.getPhoneNumber(), IO.getEmail() );
}
}
}
while(Command != 4);
}
catch(Exception e)
{
System.out.println("Invalid input");
}
}
public static void PrintMenu()
{
System.out.println();
System.out.println("Add a contact: <1>");
System.out.println("Delete a contact: <2>");
System.out.println("Display contact list: <3>");
System.out.println("Exit: <4>");
System.out.println();
}
}
class Information
{
private String FirstName;
private String LastName;
private String PhoneNumber;
private String Email;
public Information(String FirstName, String LastName, String PhoneNumber, String Email)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.PhoneNumber = PhoneNumber;
this.Email = Email;
}
public void setFirstName(String FirstName)
{
this.FirstName = FirstName;
}
public void setLastName(String LastName)
{
this.LastName = LastName;
}
public void setPhoneNumber(String PhoneNumber)
{
this.PhoneNumber = PhoneNumber;
}
public void setEmail(String Email)
{
this.Email = Email;
}
public String getFirstName()
{
return FirstName;
}
public String getLastName()
{
return LastName;
}
public String getPhoneNumber()
{
return PhoneNumber;
}
public String getEmail()
{
return Email;
}
public String PrintInformation()
{
return LastName + ", " + FirstName + " " + PhoneNumber + " " + Email;
}
}
在此程序中,它读取用户输入的数据并将其写入 contact.txt 文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class ConsoleToFile {
InputStream is;
public static void main(String[] args) throws IOException {
ArrayList<String> al = new ArrayList<String>();
File file = new File("contact.txt");
System.out.println(file);
FileOutputStream fout = new FileOutputStream(file);
Scanner sc = new Scanner(System.in);
System.out.println("enter ur name");
String name = sc.nextLine();
al.add(name);
System.out.println("enter id");
String id = sc.nextLine();
al.add(id);
System.out.println("enter phone");
String p = sc.nextLine();
al.add(p);
Iterator<String> i = al.iterator();
while (i.hasNext()) {
String s = (String) i.next() + System.lineSeparator();
System.out.println(s);
byte b[] = s.getBytes();
fout.write(b);
}
fout.close();
}
}
最后忘记关闭 PrintWriter。
使用 finally 块关闭对象。
ContactList.close();
此程序提示用户输入联系人列表要写入的文件的名称。他们输入姓名、phone 和电子邮件等信息,它会打印到屏幕并写入文件。输入的信息打印到屏幕上,文件被创建,但注释被写入其中。我错过了什么?
import java.util.*;
import java.io.*;
public class ContactInfo
{
public static void main(String[] args) throws IOException
{
try
{
int Command = 0;
String FileName = "";
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.print("Enter the name of the file for the contact list: ");
FileName = input.nextLine();
System.out.println();
File OutPutFile = new File(FileName);
PrintWriter ContactList = new PrintWriter(OutPutFile);
ContactList.format("%15s%12s%20s\n", "Name", "Phone Number", "Email Address" );
ContactList.format("%15s%12s%20s\n", "------------", "------------", "---------------------" );
String First_Name = "";
String Last_Name = "";
String Remove_Last_Name = "";
String Phone_Number = "";
String Email_Address = "";
//Information InformationObject = new Information(String,String,String,String);
TreeMap<String, Information> Entries = new TreeMap<String, Information>();
do
{
System.out.println();
PrintMenu();
System.out.println();
System.out.print("Enter a command ");
Command = input2.nextInt();
System.out.println();
if (Command == 1)
{
System.out.print("Enter the person's first name: ");
First_Name = input.nextLine();
System.out.println();
System.out.print("Enter the person's last name: ");
Last_Name = input.nextLine();
System.out.println();
System.out.print("Enter the person's phone number: ");
Phone_Number = input.nextLine();
System.out.println();
System.out.print("Enter the person's email address: ");
Email_Address = input.nextLine();
System.out.println();
Information InformationObject = new Information(First_Name, Last_Name, Phone_Number, Email_Address);
Entries.put(InformationObject.getLastName(), InformationObject );
}
if (Command == 2)
{
System.out.print("Enter the last name of the person whom you want to delete from the list: ");
Remove_Last_Name = input.nextLine();
System.out.println();
Entries.remove(Remove_Last_Name);
}
if (Command == 3)
{
for (Map.Entry Document : Entries.entrySet() )
{
Information IO = Entries.get( Document.getKey());
System.out.println( IO.getLastName() + ", " + IO.getFirstName() + " " + IO.getPhoneNumber() + " " + IO.getEmail() );
ContactList.format("%15s%12s%20s\n", IO.getFirstName() + IO.getLastName(), IO.getPhoneNumber(), IO.getEmail() );
}
}
}
while(Command != 4);
}
catch(Exception e)
{
System.out.println("Invalid input");
}
}
public static void PrintMenu()
{
System.out.println();
System.out.println("Add a contact: <1>");
System.out.println("Delete a contact: <2>");
System.out.println("Display contact list: <3>");
System.out.println("Exit: <4>");
System.out.println();
}
}
class Information
{
private String FirstName;
private String LastName;
private String PhoneNumber;
private String Email;
public Information(String FirstName, String LastName, String PhoneNumber, String Email)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.PhoneNumber = PhoneNumber;
this.Email = Email;
}
public void setFirstName(String FirstName)
{
this.FirstName = FirstName;
}
public void setLastName(String LastName)
{
this.LastName = LastName;
}
public void setPhoneNumber(String PhoneNumber)
{
this.PhoneNumber = PhoneNumber;
}
public void setEmail(String Email)
{
this.Email = Email;
}
public String getFirstName()
{
return FirstName;
}
public String getLastName()
{
return LastName;
}
public String getPhoneNumber()
{
return PhoneNumber;
}
public String getEmail()
{
return Email;
}
public String PrintInformation()
{
return LastName + ", " + FirstName + " " + PhoneNumber + " " + Email;
}
}
在此程序中,它读取用户输入的数据并将其写入 contact.txt 文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class ConsoleToFile {
InputStream is;
public static void main(String[] args) throws IOException {
ArrayList<String> al = new ArrayList<String>();
File file = new File("contact.txt");
System.out.println(file);
FileOutputStream fout = new FileOutputStream(file);
Scanner sc = new Scanner(System.in);
System.out.println("enter ur name");
String name = sc.nextLine();
al.add(name);
System.out.println("enter id");
String id = sc.nextLine();
al.add(id);
System.out.println("enter phone");
String p = sc.nextLine();
al.add(p);
Iterator<String> i = al.iterator();
while (i.hasNext()) {
String s = (String) i.next() + System.lineSeparator();
System.out.println(s);
byte b[] = s.getBytes();
fout.write(b);
}
fout.close();
}
}
最后忘记关闭 PrintWriter。
使用 finally 块关闭对象。
ContactList.close();