Java 如何使用OutputStream在Blackberry的文本文件中插入新行?
我正在尝试创建一个应用程序,能够将文本写入Blackberry中的文本文件。我可以在文本文件中写入文本,但当我尝试写入新的文本行时,它只会覆盖我以前写入的文本。有人能帮我吗?我试图寻找周围的论坛,但没有人有我需要的具体解决方案 下面是我的代码:Java 如何使用OutputStream在Blackberry的文本文件中插入新行?,java,blackberry,java-me,Java,Blackberry,Java Me,我正在尝试创建一个应用程序,能够将文本写入Blackberry中的文本文件。我可以在文本文件中写入文本,但当我尝试写入新的文本行时,它只会覆盖我以前写入的文本。有人能帮我吗?我试图寻找周围的论坛,但没有人有我需要的具体解决方案 下面是我的代码: package filepackage; import java.io.IOException; import java.io.OutputStream; import java.util.Vector; import javax.microedit
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() );
}
}
}
}
package文件包;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.Vector;
导入javax.microedition.io.Connector;
导入javax.microedition.io.file.FileConnection;
导入net.rim.device.api.io.File;
导入net.rim.device.api.system.Application;
公共类WritingText扩展应用程序{
/**
*申请的切入点
*@param args命令行参数(未使用)
*/
公共静态void main(字符串[]args){
WritingText app=新建WritingText();
app.setAcceptEvents(假);
向量v=新向量();
v、 addElement(“Test2seconds.mp3”);
v、 addElement(“Test2seconds2.mp3”);
v、 addElement(“Test2seconds3.mp3”);
v、 addElement(“Test2seconds4.mp3”);
v、 附录(“blind_willie.mp3”);
for(int i=0;i看起来您正在为向量中的每个项创建一个新文件。请尝试在循环中移动以仅围绕写入操作:
.----
| 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() );
| }
| }
'--
----
|试一试
| {
|FileConnection fc=(FileConnection)连接器。打开(…);
|//如果未引发异常,则URI有效
|如果(!fc.exists())
| {
|fc.create();//如果文件不存在,则创建该文件
| }
|OutputStream outStream=fc.openOutputStream();
|
'-->for(int i=0;i}
|
|exptream.close();
|fc.close();
| }
|捕获(ioe异常ioe)
| {
|System.out.println(ioe.getMessage());
| }
| }
'--
谢谢!!!!!!!!!!!!!它真的解决了我的问题。非常感谢!!!:D:D:D它真的帮了我的忙。:D