java 打印到文件_Java I / O:如何将循环输出打印到一个文件...
我正在尝试将多个联系人写入并保存到一个文件中,然后将其存储为十六进制文件,但它会不断创建多个文件(每个联系人一个).我试图移动:
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 + "
" + pNumber + "
" + fact;
//outputs variables to file in hexadecimal format
PrintStream fileStream = new PrintStream(fName);
fileStream.println(toHex(utfString));
While循环中的“部分”部分,该部分将在用户说他们没有其他联系人要添加到文件之后创建文件.我要使用此代码的路径是,每个联系人都以十六进制代码形式写入文件,然后用户说他们想输入另一个联系人,因此程序在第一个联系人之后将新联系人以十六进制代码写入文件中.一.
码:
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 + "
" + pNumber + "
" + 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();
}
}
java 打印到文件_Java I / O:如何将循环输出打印到一个文件...相关推荐
- 从文件中读取数据,排序之后输出到另一个文件中
文件中有一组数据,要求排序后输出到另一个文件中去 主要有两个知识点: 排序.文件操作 C++/C代码如下: [cpp] view plaincopy #include<iostream> ...
- 文件中有一组整数,要求排序后输出到另一个文件中
这个主要复习一下文件输入输出流~~ 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- 编程时java找不到文件_java.io.FileNotFoundException:系统找不到指定的文件
java.io.FileNotFoundException:系统找不到指定的文件 所以我一直坚持这一点. 基本上,我有一个名为"word.txt"创build的文件,每当我运行该程 ...
- java 中数组的创建 数组遍历 以及数组的输出(打印)
什么是数组? 如果我们需要创建一个 int 类型变量,那么我们只需要 int a; 如果我们需要创建五个 int 类型变量,那么我们只需要int a1; int a2; int a3; int a4; ...
- 单词检查:判断文件中的英文单词是否正确,若不正确,排序输出到另一个文件中
文章目录 问题描述 思路 代码 问题描述 已知有一个正确单词索引表(保存在当前目录下的文件index.txt中,且全为小写字母,按照字典序由小到大排列,每个单词独占一行),编写程序利用该单词表对某一英 ...
- php利用ajax文件上传,如何在PHP中利用AjaxForm实现一个文件上传功能
如何在PHP中利用AjaxForm实现一个文件上传功能 发布时间:2020-12-18 14:52:38 来源:亿速云 阅读:94 作者:Leah 如何在PHP中利用AjaxForm实现一个文件上传功 ...
- python把模块装到文件夹中_把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案...
把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功 解决办法是: 在该文件夹下加入空文件__init__.py python2会把 ...
- arm中的.a文件如何产生的_如何在IPFS中Pin一个文件?
对IPFS中,有这样一个误解----认为存储在其中的文件是永久保存的.实际上并不是这样,存储在IPFS中的文件只在某种特殊状态下才会被一直保存,这个特殊状态就是"Pin". &qu ...
- 文件卸载不干净或删除显示文件夹在另一个文件打开
1卸载软件后,发现软件依然存在,如删除一个流氓软件文件所在的文件夹,显示文件在另一个文件夹已打开 1)如用关键词"百度输入法"进行查杀: ① 进入" 注册表编辑器&quo ...
最新文章
- CTFshow 命令执行 web43
- ERP顾问在甲方好还是乙方好?
- invalid floating point operation什么意思_数据可视化有意思的小例子:Taylor Swift 歌词数据分析和可视化...
- JS中DOM节点的CRUD
- jvm分配内存_为JVM分配内存:一个案例研究
- curl 访问不到html_嵌入式工程师入门前后端系列1:访问一个网页
- 透明怎么弄_最新版微信如何设置透明背景?这样设置,效果令人惊喜
- php-cli和php-fpm FastCgi与PHP-fpm之间的关系
- 【司法比赛】第三届“中国法研杯”司法人工智能挑赛(CAIL2020)即将启动
- 高精度地图技术与展望
- ffplay源码编译
- Postman安装失败,解决方法
- Drozer的安装和使用教程(入门)
- 联想重装系统去掉保护_解决联想硬盘保护系统忘记密码问题,重新安装Windows10系统...
- python gif转jpg_PIL-将GIF帧转换为JPG
- 洛谷 P5713	【深基3.例5】洛谷团队系统 C语言
- 2022年8月最新运维面试题-服务器上下架流程
- Z3735d android x86,首款搭载Z3735处理器 神秘平板被曝光
- java中String优化之intern()方法
- win10控制面板快捷键_你没玩过的全新版本Win10,这些操作你知多少?