回答編集履歴

2

変数名修正

2017/11/26 07:17

投稿

keicha_hrs
keicha_hrs

スコア6768

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  public class MainActivity extends AppCompatActivity {
20
20
 
21
- private String fileName = "file.txt";
21
+ private String mFileName = "file.txt";
22
22
 
23
23
 
24
24
 
@@ -32,7 +32,7 @@
32
32
 
33
33
 
34
34
 
35
- File file = new File(getFilesDir() + "/" + fileName);
35
+ File file = new File(getFilesDir() + "/" + mFileName);
36
36
 
37
37
  if(!file.exists()){
38
38
 

1

サンプル追加

2017/11/26 07:16

投稿

keicha_hrs
keicha_hrs

スコア6768

test CHANGED
@@ -3,3 +3,53 @@
3
3
 
4
4
 
5
5
  場所は`/data/data/<パッケージ名>/files/`の下です。
6
+
7
+
8
+
9
+ ---
10
+
11
+
12
+
13
+ 起動時にファイルがあるか確認して存在しなければ空ファイルを作るサンプルです。getFilesDir()で、上記のディレクトリーのパスを得られます。
14
+
15
+
16
+
17
+ ```Java
18
+
19
+ public class MainActivity extends AppCompatActivity {
20
+
21
+ private String fileName = "file.txt";
22
+
23
+
24
+
25
+ @Override
26
+
27
+ protected void onCreate(Bundle savedInstanceState) {
28
+
29
+ super.onCreate(savedInstanceState);
30
+
31
+ setContentView(R.layout.activity_main);
32
+
33
+
34
+
35
+ File file = new File(getFilesDir() + "/" + fileName);
36
+
37
+ if(!file.exists()){
38
+
39
+ try {
40
+
41
+ file.createNewFile();
42
+
43
+ } catch (IOException e) {
44
+
45
+
46
+
47
+ }
48
+
49
+ }
50
+
51
+ }
52
+
53
+ }
54
+
55
+ ```