所以我一直试图找到一种方法来保存文件的“长”值。我正在使用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。
答案 0 :(得分:2)
要将其顺式写入纯文本文件,请使用BufferedWriter
并写下每行的值,如下所示:
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
不会给你一种人类可读的格式。)
答案 1 :(得分:2)
假设'文件'是你的DataOutputStream
:
for (long id : ids.getIDs()) file.writeLong(id);