Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建&;写入文件_Java_File Io_Treemap - Fatal编程技术网

Java 创建&;写入文件

Java 创建&;写入文件,java,file-io,treemap,Java,File Io,Treemap,此程序提示用户输入要写入联系人列表的文件名。他们输入姓名、电话和电子邮件等信息,然后打印到屏幕上,并写入文件。输入的信息打印到屏幕上并创建文件,但没有写入任何内容。我错过了什么 import java.util.*; import java.io.*; public class ContactInfo { public static void main(String[] args) throws IOException { try { int

此程序提示用户输入要写入联系人列表的文件名。他们输入姓名、电话和电子邮件等信息,然后打印到屏幕上,并写入文件。输入的信息打印到屏幕上并创建文件,但没有写入任何内容。我错过了什么

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;
    }











}
import java.util.*;
导入java.io.*;
公共类联系人信息
{
公共静态void main(字符串[]args)引发IOException
{
尝试
{
int命令=0;
字符串FileName=“”;
扫描仪输入=新扫描仪(System.in);
扫描仪输入2=新扫描仪(System.in);
System.out.print(“输入联系人列表的文件名:”);
FileName=input.nextLine();
System.out.println();
文件输出文件=新文件(文件名);
PrintWriter ContactList=新的PrintWriter(输出文件);
联系人列表。格式(“%15s%12s%20s\n”、“姓名”、“电话号码”、“电子邮件地址”);
ContactList.format(“%15s%12s%20s\n”、“----------------”、“-----------”、“-----------”;
字符串First_Name=“”;
字符串Last_Name=“”;
字符串Remove_Last_Name=“”;
字符串Phone_Number=“”;
字符串Email_Address=“”;
//信息对象=新信息(字符串,字符串,字符串,字符串);
TreeMap条目=新的TreeMap();
做
{
System.out.println();
打印菜单();
System.out.println();
系统输出打印(“输入命令”);
Command=input2.nextInt();
System.out.println();
如果(命令==1)
{
System.out.print(“输入此人的名字:”);
First_Name=input.nextLine();
System.out.println();
System.out.print(“输入此人的姓氏:”);
Last_Name=input.nextLine();
System.out.println();
System.out.print(“输入此人的电话号码:”);
Phone_Number=input.nextLine();
System.out.println();
System.out.print(“输入此人的电子邮件地址:”);
Email_Address=input.nextLine();
System.out.println();
信息对象=新信息(姓名、姓氏、电话号码、电子邮件地址);
Entries.put(InformationObject.getLastName(),InformationObject);
}
如果(命令==2)
{
System.out.print(“输入要从列表中删除的人的姓氏:”;
删除\u Last\u Name=input.nextLine();
System.out.println();
条目。删除(删除姓氏);
}   
如果(命令==3)
{
for(Map.Entry文档:Entries.entrySet())
{
信息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(命令!=4);
}
捕获(例外e)
{
System.out.println(“无效输入”);
}
}
公共静态无效打印菜单()
{
System.out.println();
System.out.println(“添加联系人:”);
System.out.println(“删除联系人:”);
System.out.println(“显示联系人列表”);
System.out.println(“出口:”);
System.out.println();
}
}
班级信息
{
私有字符串名;
私有字符串LastName;
私有字符串电话号码;
私人字符串电子邮件;
公共信息(stringfirstname、stringlastname、stringphonenumber、stringemail)
{
this.FirstName=FirstName;
this.LastName=LastName;
this.PhoneNumber=电话号码;
this.Email=电子邮件;
} 
public void setFirstName(字符串FirstName)
{
this.FirstName=FirstName;
}
public void setLastName(字符串LastName)
{
this.LastName=LastName;
}
public void setPhoneNumber(字符串PhoneNumber)
{
this.PhoneNumber=电话号码;
}
公用电子邮件(字符串电子邮件)
{
this.Email=电子邮件;
}
公共字符串getFirstName()
{
返回名字;
}
公共字符串getLastName()
{
返回姓氏;
}
公共字符串getPhoneNumber()
{
返回电话号码;
}
公共字符串getEmail()
{
回复邮件;
}
公共字符串PrintInformation()
{
返回LastName+,“+FirstName+”“+PhoneNumber+”“+电子邮件;
}
}

在此程序中,它读取用户输入的数据并将其写入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();
  }


 }
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.Scanner;
公共类控制台文件{
输入流为;
公共静态void main(字符串[]args)引发IOException{
ArrayList al=新的ArrayList();
File File=新文件(“contact.txt”);
System.out.println(文件);
FileOutputStream fout=新的FileOutputStream(文件);
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入您的名称”);
字符串名称=sc.nextLine();
加上(姓名);
System.out.println(“输入id”);
字符串id=sc.nextLine();
al.添加(id);
System.out.println(“输入电话”);
字符串p=sc.nextLine();
al.添加(p);
迭代器i=al.Iterator();
虽然
ContactList.close();