2011-09-07 58 views
1

我正在尝试创建一个能够将文本写入黑莓文本文件的应用程序。我能够在文本文件中写入文本,但是当我尝试写入新的文本行时,它只是覆盖了我之前写入的文本。谁能帮我?我试图寻找围绕论坛,但没有人有我需要的具体解决方案。如何使用OutputStream将新行插入到Blackberry的文本文件中?

下面是我的代码:

package filepackage; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Vector; 

import javax.microedition.io.Connector; 
import javax.microedition.io.file.FileConnection; 

import net.rim.device.api.io.File; 
import net.rim.device.api.system.Application; 

public class WritingText extends Application{ 
    /** 
    * Entry point for application 
    * @param args Command line arguments (not used) 
    */ 
    public static void main(String[] args){ 

     WritingText app = new WritingText(); 
     app.setAcceptEvents(false); 

     Vector v = new Vector(); 
     v.addElement("Test2seconds.mp3"); 
     v.addElement("Test2seconds2.mp3"); 
     v.addElement("Test2seconds3.mp3"); 
     v.addElement("Test2seconds4.mp3"); 
     v.addElement("blind_willie.mp3"); 

     for(int i=0;i<v.size();i++){ 
     try 
     { 
      FileConnection fc = (FileConnection)Connector.open("file:///SDCard/newfile.txt"); 
      // If no exception is thrown, then the URI is valid, but the file may or may not exist. 
      if (!fc.exists()) 
      { 
       fc.create(); // create the file if it doesn't exist 
      } 
      OutputStream outStream = fc.openOutputStream(); 
      outStream.write(((String) v.elementAt(i)).getBytes()); 

      String br = "\r\n"; 
      outStream.write (br.getBytes()); 

      outStream.close(); 
      fc.close(); 
     } 
     catch (IOException ioe) 
     { 
      System.out.println(ioe.getMessage()); 
     } 
     } 
    } 
} 

请帮助我。 :(

回答

3

看起来你正在为向量中的每个项目的新文件,尝试在循环移动仅包围写操作:

.---- 
|  try 
|  { 
|  FileConnection fc = (FileConnection)Connector.open(...); 
|  // If no exception is thrown, then the URI is valid 
|  if (!fc.exists()) 
|  { 
|   fc.create(); // create the file if it doesn't exist 
|  } 
|  OutputStream outStream = fc.openOutputStream(); 
| 
'--> for(int i=0;i<v.size();i++){ 

     outStream.write(((String) v.elementAt(i)).getBytes()); 

     String br = "\r\n"; 
     outStream.write (br.getBytes()); 
.--> } 
| 
|  outStream.close(); 
|  fc.close(); 
|  } 
|  catch (IOException ioe) 
|  { 
|  System.out.println(ioe.getMessage()); 
|  } 
| } 
'-- 
+0

谢谢!!!!!!!! !!!!真的解决了我的问题,非常感谢!!!:D:D:D真的对我有帮助:D – TEN

+0

呵呵,没问题,不客气:-) – aioobe

相关问题