2013-02-19 68 views
0

我决定使用内部存储器将文件保存到文件夹中。我的代码是:访问在内部存储器中创建的文件

File dir = new File (getFilesDir(), "myFolder"); 
dir.mkdirs(); 
File file = new File(dir, "myData.txt"); 

try { 
    FileOutputStream f = new FileOutputStream(file); 
    PrintWriter pw = new PrintWriter(f); 
    pw.println("Hi"); 
    pw.println("Hello"); 
    pw.flush(); 
    pw.close(); 
    f.close(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 

} catch (IOException e) { 
    e.printStackTrace(); 
} 
tv.append("\n\nFile written to "+file); 

显示的目录是数据/数据/包/文件/ MyFolder中/ myData.txt 不过,我想检查文件是否包含输入的字符串,但我似乎无法找到创建的文件在哪里。我试图输出文件的内容,但我失败了。我知道这听起来很荒谬。我想要做的是检查内容并知道文件是否被创建。任何人都可以帮忙吗?

回答

0

末添加该代码,

try { 
     File file = new File(dir, "myData.txt"); 
     Scanner sc = new Scanner(file); 

     while(sc.hasNext()){ 
      System.out.println(sc.nextLine()); 
     } 

    } catch (FileNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
0

从文件中打开输入流并逐行读取。

FileInputStream f = new FileInputStream(new File(getFilesDir()+"/myFolder/"+"myData.txt")); 
BufferedInputStream bis = new BufferedInputStream(fis); 
DataInputStream dis = new DataInputStream(bis); 
while (dis.available() != 0) { 
    System.out.println(dis.readLine()); 
} 
+0

OK,谢谢!有没有办法手动找到并检查创建的文件的内容? – chArm 2013-02-19 18:38:00

相关问题