【问题标题】:jcifs cannot write new line to text filejcifs 无法将新行写入文本文件
【发布时间】:2013-09-27 14:03:43
【问题描述】:

我正在将一个文件从 android 设备写入 windows 文件夹
所有内容都完好无损 但我无法写新行

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,nam,p);
    file = new SmbFile(url, auth);

    SmbFileOutputStream out = new SmbFileOutputStream(file,true);

我试过了:

out.write(("next"+ "\n" + text).getBytes());               
out.write(("next"+String.format("%n") + text).getBytes());
out.write(("next"+System.getProperty("line.separator") + text).getBytes());         
out.flush();
out.close();

但似乎没有任何工作,请帮助。

【问题讨论】:

  • 只是在这里猜测,但您尝试过\r\n 吗?或者,您可以使用PrintWriter 包装out 变量并使用其println() 方法
  • 确定@gnobal! 我已经尝试过: 在上面的代码中,仔细看它确实存在
  • PrintWriter pw = new PrintWriter(out); pw.println(); 根本没有写文字,因为它的SmbFileOutputStream 而不是java OutputStream 我认为

标签: java android samba jcifs


【解决方案1】:

这就是我用新行写入文件的方式

                Date now = new Date();
                String strDateFormat = "EEEE, MMMM dd, yyyy HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
                String txtVal = "INFO\t" + usr + "\t" + sdf.format(now) + "\t" +count +"\t"+"Ver.1";            
                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("XXXX", "XXXXX", "XXXXXX");
                String path = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                SmbFile sFile = new SmbFile(path, auth);
                //SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);

                SmbFileOutputStream sfos = new SmbFileOutputStream(sFile, true);                

                sfos.write((txtVal+"\r\n").getBytes());               
                sfos.close();

【讨论】: