【问题标题】:how to write SimpleDateFormat fo file in Android如何在 Android 中编写 SimpleDateFormat fo 文件
【发布时间】:2011-09-16 21:33:32
【问题描述】:

我是安迪世界的新手。我正在做我的第一个应用程序。 GPS追踪器。 我需要将坐标 +(时间、日期)保存到 SD 上。

坐标很容易,但时间和日期(SimpleDateFormat)给我带来了麻烦。

write() 方法与 SDF 有问题。我尝试过转换为字符串和许多其他东西。

官方报错信息是: "FileOutputStream 类型中的 write(byte[]) 方法不适用于参数 (String)"

  FileOutputStream fos = openFileOutput("kris.txt", Context.MODE_PRIVATE);
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd' 'HH:mm:ss " ); 
    String k = sdf.toString();
    fos.write(k);
    fos.flush();
    fos.close();

【问题讨论】:

    标签: android file time simpledateformat


    【解决方案1】:

    据我了解,您只需要更改方法即可。要将字符串写入文件,您需要使用 BufferedWrite 或 PrintWriter。

    试试这个:

    public void writeLinesToFile(String filename,
                                   String[] linesToWrite,
                                   boolean appendToFile) {
        PrintWriter pw = null;
        try {
          if (appendToFile) {
            //If the file already exists, start writing at the end of it.
            pw = new PrintWriter(new FileWriter(filename, true));
          }
          else {
            pw = new PrintWriter(new FileWriter(filename));
          }
          for (int i = 0; i < linesToWrite.length; i++) {
            pw.println(linesToWrite[i]);
          }
          pw.flush();
        }
        catch (IOException e) {
          e.printStackTrace();
        }
        finally {
          //Close the PrintWriter
          if (pw != null)
            pw.close();
        }
      }
    

    (来自java手册的代码)

    【讨论】:

    • 好的,如果其他人也在寻找同样的东西,你能接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2013-07-22
    • 2013-08-12
    • 2012-03-05
    相关资源
    最近更新 更多