在开发Android应用时有时会用到Input/OutputStream,这两个流的作用分别是读取文件内容,写入文件内容,其中需要注意两点:1)注意文件的编码格式,一般使用utf-8
2)写入和读取的文件时存储在内存中的,所以对文件的大小有所限制
下面这个例子实现的是:
读取编辑框中输入的内容存储到文件file.txt中,之后读取文件中的内容,实现的截图如下所示:
实现的代码如下所示:
public class OutInputStream_Activity_Test extends Activity {
private Button button;
private Button button2;
private EditText editText;
private TextView textView;
String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_out_input_stream__activity__test);
button=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
editText=(EditText)findViewById(R.id.edittext);
textView=(TextView)findViewById(R.id.textview);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
OutputStream os = null;
try {
os = openFileOutput("file.txt", Activity.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
string=editText.getText().toString();
try {
os.write(string.getBytes("utf-8"));
editText.setText("");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
InputStream is=openFileInput("file.txt");
byte [] buffer=new byte[100];
int byteCount=is.read(buffer);
String string=new String(buffer, 0, byteCount, "utf-8");
textView.setText(string);
is.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
}
分享到:
相关推荐
android-serialport-api Accessing serial ports for Android The current Android SDK does not provide any API for reading and writing to the ...https://code.google.com/archive/p/android-serialport-api/
总结了java中的基础知识,十分全面,适合初学...按照操作数据的不同可以分为字节流和字符流 按照数据传输方向的不同可以分为输入流和输出流 字节流------输入 InputStream/OutputStream 字符流------输入Reader /Writer
IO流文档,相关IO流的说明文档.............
| mark操作记录输入流中的某个点,reset操作在从包含的输入流获取新字节前,再读取最后一次。 |--DataInputStream/:用于操作基本数据类型数据的对象,应用程序可以使用数据输入流写入稍后有数据输入流读取的数据...
OutputStream out = new FileOutputStream(destFilePath); ExcelUtils.export(templateFilePath, out); System.out.println("destFilePath="+destFilePath); } catch (ExcelException e) { e.printStackTrace...
> 能够生成low poly风格的图片,Android版本的实现 效果图 使用方法 LowPoly.generate(inputStream,outputStream); 或者 /** * 生成low poly风格的图片 * * @param inputStream 源图片 * @param outputStream ...
Android multi-task file download engine. > 中文文档 DEMO Installation FileDownloader is installed by adding the following dependency to your build.gradle file: dependencies { compile '...
OutputStream os = new FileOutputStream(outputFile); BMPEncodeParam param = new BMPEncodeParam(); ImageEncoder enc = ImageCodec.createImageEncoder("BMP", os, param); enc.encode(src); os.close(); } }
InputStream and OutputStream I/O Multiple Serial ports Projects using UsbSerial Support UsbSerial If UsbSerial helped you with your projects please consider donating a little sum Or consider buying ...
关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream
is a InputStream/OutputStream lib for reading & writing file on . It's not very convenient for end-user. So, we build a HTTP-REST Service to simplify the operation. Basically, we use HEAD, GET, PUT, ...
支持的功能有: 列出设备上可用的串行端口,包括配置串行端口(波特率、停止位、权限等)的 USB 到串行适配器,提供标准的 InputStream 和 OutputStream Java 接口这个项目是不可能的: 通过 USB 从接口接收/发送...
OutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(head); //文件正文部分 DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] ...
在进行Android开发的过程中,免不了,要开发TCP/UDP通讯的程序,下面这两段代码,分别介绍了TCP/UCP通过的一个实例: 代码一: private void tcpdata() { try { Socket s = new Socket("192.168.0.25", 65500); ...
OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); System.out.println("--输出流--:"+toClient); System.out.println(...
java版s源码json-数据生成器 json-data-generator 可帮助您构建用于测试数据目的的 json 数据。 它有许多不错的功能,您可以使用它们来构建所需的尽可能多的测试数据。...outputStream); 或者 JsonDataGenerator
Java中OutputStream类数据操作方法.pdf 学习资料 复习资料 教学资源
Android 上传文件至服务器和下载文件至本地,亲测有效,只需传入相关参数即可。/** * android上传文件到服务器 * * @param file * 需要上传的文件 * @param RequestURL * 请求的rul * @return 返回...
操作方式与普通java相似,即打开一个FileInputStream/FileOutPutStream,转成InputStream/outPutStream 然后读/写字节. SharedPrefreence主要用来存储简单数据类型. 不能存文件也不适合,比如QQ第一次登陆后可以保存...
从 code.google.com/p/android-serialport-api 自动导出 目前的Android SDK 没有提供任何对Linux TTY 串口进行读写的API。 您可能会在 HTC Android 手机的连接器上找到此类串口。 该项目希望提供一个简单的 API 来...