以特定格式将数据存储在txt文件中 [英] Storing data in txt file in specific format

查看:117
本文介绍了以特定格式将数据存储在txt文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与之前的问题有关(如何以特定格式显示txt文件中的数据).

my question is related to my previous question (How to display data from txt file in specific format).

我想知道是否有可能首先以特定格式将数据存储在txt文件中,而不是先存储然后再取回并以特定格式显示它?

I was wondering if it is possible at the first place, to store data on txt file in specific format rather than store it first and then retreived it again and display it in specific format?

例如而不是像这样将数据存储在txt文件中

e.g. instead of store data on txt file like this


Jessica 
Walking
20 minutes
Matthew
Run 
10 minutes

我想以这种格式将其存储在txt文件中

I wanted to store it in txt file in this format


Jessica   Walking    20 minutes
Matthew   Run        10 minutes 

推荐答案

关于您对阿德尔的答案的评论:

Regarding your comment to adeel's answer:

感谢adeel825,但是我不知道将"\ t"放在哪里..到目前为止,我使用这种方法:new PrintStream(fout).println(name);新的PrintStream(fout).println(练习);新的PrintStream(fout).println("10分钟");

Thanks adeel825, however I dont know where to put the "\t".. so far I use this method: new PrintStream(fout).println (name); new PrintStream(fout).println (exercise); new PrintStream(fout).println("10 minutes");

首先,不要在每次打印内容时都调用"new PrintStream(fout)".这样做:

First, don't call "new PrintStream(fout)" everytime you print something. Do this:

PrintStream ps = new PrintStream(fout);
ps.print(name);
ps.print('\t');
ps.print(exercise);
ps.print('\t');
ps.print(time);
ps.println();

或者简单地:

PrintStream ps = new PrintStream(fout);
ps.println(name + '\t' + exercise + '\t' + time);

修改

针对您的评论:

还有一个问题...某些名称太长,需要更多的制表符..我放了ps.print('\ t','\ t');但它似乎不起作用.

one more question...some of the name are too long and it requires more tab..I have put ps.print('\t','\t'); but it seems not working..

如果这是一个问题,听起来您正在尝试以想要显示它们的方式来存储它们.我以为您正在尝试以一种易于以编程方式解析的方式来存储它们.如果您希望将它们存储在列中,我建议使用空格而不是制表符填充.

If this is a problem, it sounds like you are trying to store them in the manner you want to display them. I had assumed you were trying to store them in a way that would be easy to parse programmatically. If you want to store them displayed in columns, I'd suggest padding with spaces rather than tabs.

如果您知道所有列的宽度都小于30个字符,则可以使用printf做类似的事情:

If you know that all the columns are going to be less than, say, 30 characters wide, you could do something like this with printf:

ps.printf("%30s%30s%30s%n", name, exercise, time);

如果您不习惯该语法的话,它看起来就好像拜占庭式的.基本上每个%30s"表示都应填充字符串参数,以便其宽度至少为30个字符.如果任何一个值的宽度为30个或更多字符,则结果将看起来不正确.如果您无法提前知道,则必须遍历每列中的值以确定所需的列宽.

That syntax can look quite byzantine if you're not used to it.. basiclly each "%30s" means pad the string argument so that it is at least 30 characters wide. Your result won't look right if any of the values are 30 or more characters wide. If you can't know ahead of time, you'll have to loop through the values in each column to determine how wide the columns need to be.

这篇关于以特定格式将数据存储在txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆