如何将数据保存到Java中的文件? [英] How to save the data into File in java?

查看:184
本文介绍了如何将数据保存到Java中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,那就是我有一串数据,我想每次都把它保存到一个单独的文件中。请给我一个建议。



感谢,
vara kumar.pjd

解决方案

使用de文件名中的时间戳记,以确保它是唯一的。 *;
class FileWrite
{
public static void main(String args [])
{
try {
//创建文件
FileWriter fstream = new FileWriter(System.currentTimeMillis()+out.txt);
BufferedWriter out = new BufferedWriter(fstream);
out.write(Hello Java);
//关闭输出流
out.close();
} catch(Exception e){//捕获异常,如果有
System.err.println(Error:+ e.getMessage());





这是非常很容易google。


I have one problem, that is I have one string of data and I want to save it into a separate file every time. Please give me a suggestion.

Thanks, vara kumar.pjd

解决方案

Use a timestamp in de filename so you can be sure it is unique.

import java.io.*;
class FileWrite 
{
   public static void main(String args[])
  {
      try{
    // Create file 
    FileWriter fstream = new FileWriter(System.currentTimeMillis() + "out.txt");
        BufferedWriter out = new BufferedWriter(fstream);
    out.write("Hello Java");
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

This is something that is very easy to google.

这篇关于如何将数据保存到Java中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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