Я пытаюсь записать и сохранить несколько контактов в один файл, а затем сохранить его как файл Hex, но он поддерживает создание нескольких файлов (по одному на контакт). Я пытался переместить:Ввод/вывод Java: как печатать контуры вывода в один файл?
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
//append information to the string to be converted
utfString = name + "\r\n" + pNumber + "\r\n" + fact;
//outputs variables to file in hexadecimal format
PrintStream fileStream = new PrintStream(fName);
fileStream.println(toHex(utfString));
раздел в то время цикла, который будет создавать файл после того, как пользователь говорит, что они не имеют больше контактов, чтобы добавить в файл. Путь, который я хочу взять с этим кодом, состоит в том, что каждый контакт записывается в файл как шестнадцатеричный код, а затем пользователь говорит, что хочет ввести другой контакт, поэтому программа записывает новый контакт в файл в шестнадцатеричном коде сразу после первого один.
Код:
import java.util.Scanner;
import java.io.PrintStream;
import java.io.IOException;
public class Exercise27{
public static String toHex(String currString){
//converts a string to a byte-array
byte[] preConvert = currString.getBytes();
//converts byte-array to hexadecimal string using a function from the javax library included in Java Platform SE 7 and onwards
return javax.xml.bind.DatatypeConverter.printHexBinary(preConvert);
}
public static void main(String[] args) throws IOException{
//starts keyboard input
Scanner kbReader = new Scanner(System.in);
Boolean MoreContacts = true;
String More;
do{
//declare variable to convert to hex later on
String utfString = "";
//create variables for user-information
System.out.print("Type your full name, followed by hitting 'enter': ");
String name = kbReader.nextLine();
System.out.print("Type your phone number, followed by hitting 'enter': ");
String pNumber = kbReader.nextLine();
System.out.print("Type one interesting fact about yourself, followed by hitting 'enter': ");
String fact = kbReader.nextLine();
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
//append information to the string to be converted
utfString = name + "\r\n" + pNumber + "\r\n" + fact;
//outputs variables to file in hexadecimal format
PrintStream fileStream = new PrintStream(fName);
fileStream.println(toHex(utfString));
System.out.println("More contacts? (Enter y or n)");
MoreContacts = false;
More = kbReader.nextLine();
System.out.println("More: " + More);
if((More.equalsIgnoreCase("y")) || (More.equalsIgnoreCase("yes")))
{
MoreContacts = true;
}
}while(MoreContacts);
PrintStream fileStream;
//close your systems
//fileStream.close();
kbReader.close();
}
}