这只是创建文件并将其写入项目目录的早期测试。我认为这会很快,但要么我做错了什么,要么我没有正确更新某些东西?
我想做的就是能够动态地创建一个文件并将其写入我的原始文件夹。
我对其他事情使用了几种方法,例如获取对象的 ArrayList,使用 for 循环单独获取它们,然后使用“write”写入文件。
这是我的代码:我要创建和写入的文件称为 txt 文件,称为 output。
我真的很感谢有人帮我看这个,非常感谢!
//for writing file...
public static void writeGrids(List<Grid> grids)
{
for(int i = 0; i < grids.size(); i++)
{
try {
write(grids.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static void write(Grid grid) throws IOException
{
try
{
FileWriter fstream = new FileWriter("/My Project/res/raw/output.txt");
BufferedWriter out = new BufferedWriter(fstream);
//FILE DIR!!!!!!
out.write(grid.gridNickName + ";" + grid.gridName + ";" +
grid.firstName + ";" + grid.lastName + ";" +
grid.password + ";" + grid.loginURI + ";");
out.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
这是尝试将某些内容保存到内部存储的更新版本。这会是一个理想的解决方案吗?
更新代码:
//for writing file...
public void writeGrids(List<Grid> grids)
{
for(int i = 0; i < grids.size(); i++)
{
try {
write(grids.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void write(Grid grid) throws IOException
{
try
{
FileOutputStream fos;
String FILENAME = "InternalString";
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
String data = grid.getGridNickName() + ";" + grid.getGridName() + ";" +
grid.getFirstName() + ";" + grid.getLastName() + ";" +
grid.getPassword() + ";" + grid.getLoginURI() + ";";
fos.write(data.getBytes());
fos.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
用户会调用 write 方法来开始这样的事情.. writeGrids(myNewArrayList);