カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

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

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

String PATH = Environment.getExternalStorageDirectory().toString() + "/DCIM/Example";

File f = new File(PATH);

if(!f.exists()) {
    try {
        f.mkdir();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
6,191 views

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

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

InputStream inputStream = null;
BufferedReader reader = null;
try {
    inputStream = getResources().getAssets().open("ファイル名");
    reader = new BufferedReader(
              new InputStreamReader(inputStream));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        Log.v("reader", line);
        if(line != ""){
            sb.append(line);
        }
    }
    String str = sb.toString();
    Toast.makeText(this, str, Toast.LENGTH_LONG).show();

} catch (IOException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
7,058 views

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

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

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

File inputFile  = new File("入力元ファイルのパス");
File outputFile = new File("出力先ファイルのパス");

try {
    FileChannel inputChannel  = new FileInputStream(inputFile).getChannel();
    FileChannel outputChannel = new FileOutputStream(outputFile).getChannel();

    // インプットチャネルの出力をアウトプットチャネルに接続
    inputChannel.transferTo(0, inputChannel.size(), outputChannel);

    inputChannel.close();
    outputChannel.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
2,698 views

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

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

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

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

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

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

private void readFile(String filename) {

    FileInputStream inputStream = null;
    BufferedReader reader = null;

    try {
        inputStream = new FileInputStream(filename);
        reader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            //Log.v(TAG, line);
            //読み込んだデータの処理を記述
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

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

private void writeFile(String filename, String src){
    try {
        FileOutputStream outputStream = new FileOutputStream(filename);
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(outputStream));
        writer.write(src);
        //writer.flush(); //flushでも書き込んでくれるよう
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
2,572 views

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

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

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")

'ファイルをコピー
fso.CopyFile [コピー元ファイル], _
    [コピー先ファイル], _
    [上書きフラグ(Trueで上書き:省略可)]

'フォルダーをコピー
fso.CopyFolder [コピー元フォルダー], _
    [コピー先フォルダー], _
    [上書きフラグ(Trueで上書き:省略可)]

Set fso = Nothing
3,320 views