java 写字符到文件内容,java 将字符串、list 写入到文件,并读取内容

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStreamWriter;

import java.io.StreamCorruptedException;

import java.io.UnsupportedEncodingException;

import java.util.List;

import android.graphics.Bitmap;

public class FileUtils {

/**

* 字符流写入字符串到txt

*/

@SuppressWarnings("resource")

public static void FileString(String path, String data) {

try {

FileWriter writer = new FileWriter(path);// 字符流

writer.write(data);

writer.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 字节输出到txt

*

* @param path

* @param data

*/

@SuppressWarnings("resource")

public static void FileString2(String path, String data) {

try {

FileOutputStream outputStream = new FileOutputStream(path);// 字节流

outputStream.write(data.getBytes());

outputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 设置编码格式写出到txt

*

* @param path

* @param data

*/

public static void FileString3(String path, String data) {

try {

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");// 设置编码格式

writer.write(data);

writer.close();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 追加写入到txt

*

* @param path

* @param data

*/

@SuppressWarnings("resource")

public static void FileString4(String path, String data) {

try {

FileOutputStream outputStream = new FileOutputStream(path, true);// 追加写入

outputStream.write(("\r\n" + data).getBytes());

outputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 存储list到文件

*

* @param path

* @param list

*/

@SuppressWarnings("resource")

public static void FileWriteList1(String path, Listlist) {

try {

FileOutputStream outputStream = new FileOutputStream(path);

ObjectOutputStream stream = new ObjectOutputStream(outputStream);

stream.writeObject(list);

stream.close();

outputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 设置编码格式存储list到txt

*

* @param path

* @param list

*/

@SuppressWarnings("resource")

public static void FileWriteList(String path, Listlist) {

try {

BufferedWriter bufferedWriter = new BufferedWriter(

new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));

for (T s : list) {

bufferedWriter.write(s.toString());

bufferedWriter.newLine();

bufferedWriter.flush();

}

bufferedWriter.close();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* bitmap 写入到本地

*

* @param path

* @param bitmap

*/

@SuppressWarnings("resource")

public static void FileBitmap(String path, Bitmap bitmap) {

try {

FileOutputStream outputStream = new FileOutputStream(path);

bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

outputStream.flush();

outputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 读取本地文件数据设置指定编码

*

* @param path

*/

@SuppressWarnings("resource")

public static String FileInputString(String path) {

StringBuffer buffer = new StringBuffer();

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));

String data = null;

while ((data = reader.readLine()) != null) {

buffer.append(data + "\r\n");

}

reader.close();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return buffer.toString();

}

/**

* 根据字节读取文件

*

* @param path

* @return

*/

@SuppressWarnings("resource")

public static String FileInputString2(String path) {

StringBuffer buffer = new StringBuffer();

try {

FileInputStream inputStream = new FileInputStream(path);

byte[] bytes = new byte[1024];

int bytead = 0;

while ((bytead = inputStream.read(bytes)) != -1) {

buffer.append(new String(bytes, 0, bytead));

}

inputStream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return buffer.toString();

}

/**

* 获取本地文件中的list

*

* @param path

*/

@SuppressWarnings("resource")

public static void FileInputList(String path) {

try {

FileInputStream inputStream = new FileInputStream(path);

ObjectInputStream stream = new ObjectInputStream(inputStream);

Listlist = (List) stream.readObject();

inputStream.close();

stream.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (StreamCorruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 高效读取指定编码格式的文件

* @param path

* @return

*/

@SuppressWarnings("resource")

public static String FileInput3(String path) {

StringBuffer buffer = new StringBuffer();

try {

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(new FileInputStream(path), "UTF-8"));

String data = null;

while ((data = bufferedReader.readLine()) != null) {

buffer.append(data+"\r\n");

}

bufferedReader.close();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return buffer.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿真,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角在于模拟环境中的真实程度。理想的模拟环境应该使用户难以分辨真假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是真的,听上去是真的,动起来是真的,甚至闻起来、尝起来等一切感觉都是真的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿真等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值