Java 如何保存哈希集<;字符串>;到.txt?
我想将哈希集存储到服务器目录。 但我现在只能将其存储在.bin文件中。 但是如何将HashSet中的所有密钥打印到一个.txt文件中呢Java 如何保存哈希集<;字符串>;到.txt?,java,string,file-io,set,hashset,Java,String,File Io,Set,Hashset,我想将哈希集存储到服务器目录。 但我现在只能将其存储在.bin文件中。 但是如何将HashSet中的所有密钥打印到一个.txt文件中呢 static Set<String> MapLocation = new HashSet<String>(); try { SLAPI.save(MapLocation, "MapLocation.bin"); } catch (Exception ex) { } public static
static Set<String> MapLocation = new HashSet<String>();
try {
SLAPI.save(MapLocation, "MapLocation.bin");
} catch (Exception ex) {
}
public static void save(Object obj, String path) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
path));
oos.writeObject(obj);
oos.flush();
oos.close();
}
static Set mapplocation=new HashSet();
试一试{
SLAPI.save(MapLocation,“MapLocation.bin”);
}捕获(例外情况除外){
}
公共静态void save(对象obj,字符串路径)引发异常{
ObjectOutputStream oos=新的ObjectOutputStream(新文件OutputStream(
路径);
oos.writeObject(obj);
oos.flush();
oos.close();
}
这将把字符串保存到UTF-8文本文件中:
public static void save(Set<String> obj, String path) throws Exception {
PrintWriter pw = null;
try {
pw = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
for (String s : obj) {
pw.println(s);
}
pw.flush();
} finally {
pw.close();
}
}
publicstaticvoidsave(setobj,stringpath)抛出异常{
PrintWriter pw=null;
试一试{
pw=新的打印编写器(
新的OutputStreamWriter(新文件OutputStream(路径),“UTF-8”);
用于(字符串s:obj){
pw.println(s);
}
pw.flush();
}最后{
关闭();
}
}
特别选择UTF-8是可取的,因为否则它将使用操作系统使用的任何设置作为默认设置,这将给您带来兼容性方面的麻烦。类似以下内容:
// check IOException in method signature
BufferedWriter out = new BufferedWriter(new FileWriter(path));
Iterator it = MapLocation.iterator(); // why capital "M"?
while(it.hasNext()) {
out.write(it.next());
out.newLine();
}
out.close();
public static void toTextFile(String fileName, Set<String> set){
Charset charset = Charset.forName("UTF-8");
try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(fileName, charset))) {
for(String content: set){
writer.println(content);
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
}
publicstaticvoidtotextfile(字符串文件名,Set){
Charset Charset=Charset.forName(“UTF-8”);
try(PrintWriter=newprintwriter(Files.newBufferedWriter(fileName,charset))){
for(字符串内容:设置){
writer.println(内容);
}
}捕获(IOX异常){
系统错误格式(“IOException:%s%n”,x);
}
}
注意:此代码是使用Java7中引入的try-with-resource构造编写的。但是,对于其他版本,这个想法也将保持不变。另一种避免文件末尾换行的解决方案:
private static void store(Set<String> sourceSet, String targetFileName) throws IOException
{
StringBuilder stringBuilder = new StringBuilder();
for (String setElement : sourceSet)
{
stringBuilder.append(setElement);
stringBuilder.append(System.lineSeparator());
}
String setString = stringBuilder.toString().trim();
byte[] setBytes = setString.getBytes(StandardCharsets.UTF_8);
Files.write(Paths.get(targetFileName), setBytes);
}
私有静态无效存储(Set sourceSet,String targetFileName)引发IOException
{
StringBuilder StringBuilder=新的StringBuilder();
for(字符串setElement:sourceSet)
{
追加(setElement);
追加(System.lineSeparator());
}
String setString=stringBuilder.toString().trim();
byte[]setBytes=setString.getBytes(StandardCharsets.UTF_8);
Files.write(path.get(targetFileName),setBytes);
}
为什么不迭代集合并将所有字符串写入文件?还有,SLAPI是什么类型的对象?哈希集中的键是什么意思?非常感谢,它完成了任务。