This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.andfileapp2; | |
import java.io.BufferedWriter; | |
import java.io.FileWriter; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import android.os.Environment; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String arg2 =""; | |
// Write | |
{ | |
try { | |
String sdPath = Environment.getExternalStorageDirectory().getPath(); | |
arg2=arg2+"\n"+sdPath; | |
String fileName = "/Hello.txt"; | |
BufferedWriter bw = null; | |
try { | |
FileWriter fw = new FileWriter(sdPath + fileName); | |
bw = new BufferedWriter(fw); | |
bw.write("hello"); | |
} finally { | |
bw.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
arg2=arg2+e.toString(); | |
} | |
} | |
String readString=""; | |
// Read | |
{ | |
try { | |
String sdPath = Environment.getExternalStorageDirectory().getPath(); | |
String fileName = "/Hello.txt"; | |
BufferedReader br = null; | |
try { | |
FileReader fr = new FileReader(sdPath + fileName); | |
br = new BufferedReader(fr); | |
StringBuilder sb = new StringBuilder(); | |
String str; | |
while((str = br.readLine()) != null){ | |
sb.append(str +"\r\n"); | |
} | |
// Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show(); | |
readString = sb.toString(); | |
} finally { | |
br.close(); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
arg2=arg2+e.toString(); | |
} | |
} | |
TextView tv = new TextView(this); | |
tv.setText("Hello, Android"+arg2+"\n"+readString); | |
setContentView(tv); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" > | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="@string/hello_world" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.andfileapp2" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="17" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.example.andfileapp2.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> | |
</manifest> |
0 件のコメント:
コメントを投稿