如何将Java long值保存到文件?

泰森

因此,我一直试图找到一种将“长”值保存到文件的方法。我正在使用Twitter4j软件包。当我获得“跟随者ID”列表时,会将其保存到一个长变量中。我可以对该值进行系统输出打印,但是不能将其写入文件。我将在代码中显示它。

//First of course I put all this stuff in. This is not the Problem though.
String consumerKey = "************"; 
String consumerSecret = "**************";
String accessToken = "*******************************";
String accessTokenSecret = "*****************";
TwitterFactory twitterFactory = new TwitterFactory();
Twitter twitter = twitterFactory.getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

//Then add the cursor value and call the command
long cursor = -1;                
IDs ids = twitter.getFollowersIDs("username_foo",cursor);

//If I want to the next cursor value
System.out.print(ids.getNextCursor());

//It works to do a system out print of the IDs
for (long id : ids.getIDs()) {System.out.println(id);}

//So how do I write it into a file? This sure don't work.
file.write(getIDs());

以防万一,您正在使用FileOutputStream和DataOutputSream。

qqilihq

要将其以逐行方式写入纯文本文件,请使用aBufferedWriter并按如下所示写入每一行值:

File outputFile = new File("/path/to/file.txt");
Writer writer = new BufferedWriter(
                new OutputStreamWriter(
                new FileOutputStream(outputFile), "UTF-8"));
for (long id : ids.getIDs()) {
    writer.write(id + "\n");
}
writer.close();

DataOutputStream相反,它不会为您提供人类可读的格式)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章