I/O流就是对文件的操作,按流来分类分为输入与输出二类,按字节和字符分类分为字节流和字符流。
输出流:字符输出流,字节输出流。
输入流:字符输入流,字节输入流。
而按字节和字符分则是,字节流和字符流都有自己的输入输出方法。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class text01 {
public static void main(String[] args) throws FileNotFoundException {
//因为是字节输入流我们会读出文件的内容,要选择找到文件的路径在FlieInputStream(“写入我们的路径”)。
但是这个类本身会抛出一个异常因此我们需要选择一个处理方式,可以继续往上抛,或则用try—catch方法进行处理。
FileInputStream fis=new FileinputStream(“输入路径”);
//在使用本方法的过程中需要一个字节数组来作为缓冲区,我们读到的字节流会先存在缓冲区中.
byte[]bytes=new byte[10240];
//我们使用字节输入流的read方法来进行读取,同时将数据读取到字节数组中.
a = fis.read(bytes);
while (a!=-1) {
String c= new String(bytes);
//读取后把字节数组可以转化为字符串,同时用循环的方法使用,以免字节数组长度达到极限造成的读取不完整.
System.out.println(c);
a= fis.read(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//最后我们要关闭流,关闭时会报一个异常,我们在这里进行处理.
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接下来我们介绍字节输出流:
字节输出流与字节输入流有很多相识之处,首先我们要做的也是导包.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class shuchuliuoutput {
public static void main(String[] args) throws IOException {
//同字节输入流一样我们需要选择文件路径,可以自定义文件夹名称,我们可以在路径后面写上true这个值,表示不覆盖原有文件内容,否则会覆盖掉之前的.
FileOutputStream fos = new FileOutputStream("D:\\writeh",true);
//创一个要输出的内容
String bc = new String("haohaohao");
//将输入的内容转化为一个字节数组
byte[] bytes=bc.getBytes();
//用FileOutputStream的write方法来输出写出,同理 此方法会有一个异常可以继续抛出或者处理.
fos.write(bytes,0,bytes.length);
//关闭流
fos.close();
}
}
我们在看字符输入流:
//其过程与字节输入流很像,只是将字节数组换成字符数组
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class zifishuchu {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\java.txt");
char[] cc = new char[1024];
int data;
while ((data=fr.read(cc))!=-1) {
String aa=new String(cc);
System.out.println(aa);
}
fr.close();
}
}
我们在看字符输出流:
import java.io.FileWriter;
import java.io.IOException;
public class writeshuchu {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("D:\\wenjian.txt");
String cc = "好好学习,天天向上";
char[] aa = cc.toCharArray();
fw.write(aa, 0, aa.length);
fw.close();
}
}
activity_main
1.getCacheDir()
输出流:字符输出流,字节输出流。
输入流:字符输入流,字节输入流。
而按字节和字符分则是,字节流和字符流都有自己的输入输出方法。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class text01 {
public static void main(String[] args) throws FileNotFoundException {
//因为是字节输入流我们会读出文件的内容,要选择找到文件的路径在FlieInputStream(“写入我们的路径”)。
但是这个类本身会抛出一个异常因此我们需要选择一个处理方式,可以继续往上抛,或则用try—catch方法进行处理。
FileInputStream fis=new FileinputStream(“输入路径”);
//在使用本方法的过程中需要一个字节数组来作为缓冲区,我们读到的字节流会先存在缓冲区中.
byte[]bytes=new byte[10240];
//我们使用字节输入流的read方法来进行读取,同时将数据读取到字节数组中.
a = fis.read(bytes);
while (a!=-1) {
String c= new String(bytes);
//读取后把字节数组可以转化为字符串,同时用循环的方法使用,以免字节数组长度达到极限造成的读取不完整.
System.out.println(c);
a= fis.read(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//最后我们要关闭流,关闭时会报一个异常,我们在这里进行处理.
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接下来我们介绍字节输出流:
字节输出流与字节输入流有很多相识之处,首先我们要做的也是导包.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class shuchuliuoutput {
public static void main(String[] args) throws IOException {
//同字节输入流一样我们需要选择文件路径,可以自定义文件夹名称,我们可以在路径后面写上true这个值,表示不覆盖原有文件内容,否则会覆盖掉之前的.
FileOutputStream fos = new FileOutputStream("D:\\writeh",true);
//创一个要输出的内容
String bc = new String("haohaohao");
//将输入的内容转化为一个字节数组
byte[] bytes=bc.getBytes();
//用FileOutputStream的write方法来输出写出,同理 此方法会有一个异常可以继续抛出或者处理.
fos.write(bytes,0,bytes.length);
//关闭流
fos.close();
}
}
我们在看字符输入流:
//其过程与字节输入流很像,只是将字节数组换成字符数组
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class zifishuchu {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\java.txt");
char[] cc = new char[1024];
int data;
while ((data=fr.read(cc))!=-1) {
String aa=new String(cc);
System.out.println(aa);
}
fr.close();
}
}
我们在看字符输出流:
import java.io.FileWriter;
import java.io.IOException;
public class writeshuchu {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("D:\\wenjian.txt");
String cc = "好好学习,天天向上";
char[] aa = cc.toCharArray();
fw.write(aa, 0, aa.length);
fw.close();
}
}
android
字节输出流基本上大同小异;
内部存储空间路径
File file=new File("data/data/com.example.wuzuo.login");
Fileoutputstream fos;
try
{
Fileoutputstream fos= new Fileoutputstream(file);
fos.write((name+pass).getbytee());
}
catch(Exception e)
{
e.printStackTrace();
}
mainactivity.java
package com.example.wuzuo.login;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void login(View v )
{
EditText ET_1= (EditText) findViewById(R.id.ET_1);
EditText ET_2= (EditText) findViewById(R.id.ET_2);
Button login= (Button) findViewById(R.id.login);
CheckBox cb= (CheckBox) findViewById(R.id.check1);
String name=ET_1.getText().toString();
String password=ET_2.getText().toString();
if(cb.isChecked())
{
File file=new File("data/data/com.example.wuzuo.login/info.text");
FileOutputStream fos;
try {
fos=new FileOutputStream(file);
fos.write((name+"##"+password).getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
System.out.println("登陆成功");
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wuzuo.login.MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ET_1"
android:hint="请输入你的用户名"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ET_2"
android:hint="请输入你的密码"
android:inputType="textPassword"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:text="记住用户名密码"
android:id="@+id/check1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="登陆"
android:textStyle="bold"
android:textColor="#000000"
android:onClick="login"
android:id="@+id/login"
/>
</RelativeLayout>
</LinearLayout>
使用API获取内部存储路径和用sharedperferance获取原有的账号密码
1.getCacheDir()
if(cb.isChecked())
{
File file=new File(getCacheDir(),"info.txt");
FileOutputStream fos;
try {
fos=new FileOutputStream(file);
fos.write((name+"##"+password).getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
getfileDir
if(cb.isChecked())
{
File file=new File(getFileDir(),"info.txt");
FileOutputStream fos;
try {
fos=new FileOutputStream(file);
fos.write((name+"##"+password).getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
sharePerferance
if(cb.isChecked())
{
SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("name",name);
editor.putString("pass",pass);
editor.commit();
}