カテゴリー
SugiBlog Webエンジニアのためのお役立ちTips

SDカードにフォルダーを作成する mkDir

SDカード内にフォルダーを作成します。
再帰的に作成するにはmkDirsメソッドを使用します。
※パーミッションの記述は割愛します。

1String PATH = Environment.getExternalStorageDirectory().toString() + "/DCIM/Example";
2 
3File f = new File(PATH);
4 
5if(!f.exists()) {
6    try {
7        f.mkdir();
8    } catch (IOException e) {
9        e.printStackTrace();
10    } catch (Exception e) {
11        e.printStackTrace();
12    }
13}
6,366 views

assetsフォルダーにあるテキストファイルを読み込む

assetsフォルダーに格納したテキストファイルを読み込みます。

1InputStream inputStream = null;
2BufferedReader reader = null;
3try {
4    inputStream = getResources().getAssets().open("ファイル名");
5    reader = new BufferedReader(
6              new InputStreamReader(inputStream));
7    String line;
8    StringBuilder sb = new StringBuilder();
9    while ((line = reader.readLine()) != null) {
10        Log.v("reader", line);
11        if(line != ""){
12            sb.append(line);
13        }
14    }
15    String str = sb.toString();
16    Toast.makeText(this, str, Toast.LENGTH_LONG).show();
17 
18} catch (IOException e) {
19    e.printStackTrace();
20} catch (SQLException e) {
21    e.printStackTrace();
22} catch (Exception e) {
23    e.printStackTrace();
24} finally {
25    try {
26        reader.close();
27        inputStream.close();
28    } catch (IOException e) {
29        e.printStackTrace();
30    }
31 
32}
7,247 views

New I/Oの高速ファイル入出力

FileクラスにはCopyなどというメソッドがないようなので、調べていたらNew I/Oというものを発見したのでメモ。

java.nio.channels.FileChannelを使用して、簡単にコピーできます。

1File inputFile  = new File("入力元ファイルのパス");
2File outputFile = new File("出力先ファイルのパス");
3 
4try {
5    FileChannel inputChannel  = new FileInputStream(inputFile).getChannel();
6    FileChannel outputChannel = new FileOutputStream(outputFile).getChannel();
7 
8    // インプットチャネルの出力をアウトプットチャネルに接続
9    inputChannel.transferTo(0, inputChannel.size(), outputChannel);
10 
11    inputChannel.close();
12    outputChannel.close();
13} catch (FileNotFoundException e) {
14    e.printStackTrace();
15} catch (IOException e) {
16    e.printStackTrace();
17}
2,839 views

ファイル入出力(SDカード)

ローカルファイルへのアクセスについては[ローカルファイルのアクセス]、[ファイル入出力 [Android]]でも紹介しています。

同様の方法でおこなうとSDカード上のファイルの読み書きはできません。
ローカルファイルを読み込み・保存する場合はopenFileOutputメソッド等を使用しますが、引数に指定するファイルパスはフルパスではなく、ファイル名のみです。
引数に「/」(パスのセパレイター)が使用できなくなっています。

[スクリーンショットの保存]でも保存方法の違いを書いていますが、改めてSDカードのファイル入出力について書いておきます。

テキストファイルの読み書きを例とします。

テキストファイルの読み込み

1private void readFile(String filename) {
2 
3    FileInputStream inputStream = null;
4    BufferedReader reader = null;
5 
6    try {
7        inputStream = new FileInputStream(filename);
8        reader = new BufferedReader(
9                new InputStreamReader(inputStream));
10        String line;
11        while ((line = reader.readLine()) != null) {
12            //Log.v(TAG, line);
13            //読み込んだデータの処理を記述
14        }
15 
16    } catch (IOException e) {
17        e.printStackTrace();
18    } catch (SQLException e) {
19        e.printStackTrace();
20    } catch (Exception e) {
21        e.printStackTrace();
22    } finally {
23        try {
24            reader.close();
25            inputStream.close();
26        } catch (IOException e) {
27            e.printStackTrace();
28        }
29 
30    }
31 
32}

テキストファイルの書き込み

1private void writeFile(String filename, String src){
2    try {
3        FileOutputStream outputStream = new FileOutputStream(filename);
4        BufferedWriter writer = new BufferedWriter(
5                new OutputStreamWriter(outputStream));
6        writer.write(src);
7        //writer.flush(); //flushでも書き込んでくれるよう
8        writer.close();
9    } catch (FileNotFoundException e) {
10        e.printStackTrace();
11    } catch (IOException e) {
12        e.printStackTrace();
13    }
14 
15}
2,723 views

FileSystemObjectを利用してファイルをコピー

Scripting.FileSystemObjectを利用してファイルをコピーする方法

1Dim fso As Object
2 
3Set fso = CreateObject("Scripting.FileSystemObject")
4 
5'ファイルをコピー
6fso.CopyFile [コピー元ファイル], _
7    [コピー先ファイル], _
8    [上書きフラグ(Trueで上書き:省略可)]
9 
10'フォルダーをコピー
11fso.CopyFolder [コピー元フォルダー], _
12    [コピー先フォルダー], _
13    [上書きフラグ(Trueで上書き:省略可)]
14 
15Set fso = Nothing
3,530 views