怎么将一个文件流转换成字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么将一个文件流转换成字符串相关的知识,希望对你有一定的参考价值。
参考技术A FormFile file = uploaldForm.getFile();String filePath ;
try
//取得上传的文件
InputStream stream = file.getInputStream();
//把文件读入
filePath = request.getRealPath("/upload");
//上传到指定的upload包中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/"
+ file.getFileName());
//建立一个上传文件的输出流
System.out.println(filePath + "/" + file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
bos.write(buffer, 0, bytesRead);
//将文件写入服务器
bos.close();
stream.close();[align=left][/align] 参考技术B public static byte[] getFileToByte(File file)
byte[] by = new byte[(int) file.length()];
try
InputStream is = new FileInputStream(file);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
byte[] bb = new byte[2048];
int ch;
ch = is.read(bb);
while (ch != -1)
bytestream.write(bb, 0, ch);
ch = is.read(bb);
by = bytestream.toByteArray();
catch (Exception ex)
ex.printStackTrace();
return by;
75. InputStreamReader和OutputStreamWriter(转换流--字节流转换成字符流)
转换流:
InputStreamReader 输入字节流转换成输入字符流
OutputStreamWriter 输出字节流转换成输出字符流
总结:就是字节流转换成字符流,但是不能字节流转换成字节流(联想记忆----只有屌丝想变成高富帅,没有高富帅想变成屌丝)
应用场景:
在以后开发中中我们免不了使用别人的工具,如果别人就是返回一个字节流,但是你想用字符流,这个时候我们就可以使用转换流来把字节流转换成字符流
下面是一些实例演示:
把字节流转换成字符流,并实现读写
public class Demo3 { public static void main(String[] args) throws IOException { writeText(); readText(); } public static void readText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); //输入字节流转换成输入字符流 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); char[] cs = new char[1024]; int length = 0; while((length = inputStreamReader.read(cs))!=-1) { System.out.println(new String(cs,0,length)); } inputStreamReader.close(); } public static void writeText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); outputStreamWriter.write("你好,陌生人"); outputStreamWriter.close(); } }
把字节流转换成字符流后用字符流的缓存类实现读写
public class Demo4 { public static void main(String[] args) throws IOException { readText(); writeText(); } public static void readText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileOutputStream fileInputStream = new FileOutputStream(file); //输出字节流转换成输入字符流 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileInputStream); //使用输出字符流的缓存类 BufferedWriter bufferedReader = new BufferedWriter(outputStreamWriter); //一行一行存 bufferedReader.write("这是第一行数据"); bufferedReader.newLine(); bufferedReader.write("这是第二行数据"); bufferedReader.close(); } public static void writeText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); //输入字节流转换成输入字符流 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); //使用输入字符流的缓存类 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line = null; //一行一行读取文本 while((line = bufferedReader.readLine())!=null) { System.out.println(line); } bufferedReader.close(); } }
把字节流转换成字符流并指定码表进行读写数据
注意:记事本值存储和我们从记事本获取数据的都是码值,并不是我们输入时的字符,只不过我们在打开记事本的时候记事本自动给我们解码了,也就是我们看的懂得字符,上面我们没有指定码表,实际上是读写时使用的是默认的编码表gbk(java中默认码表)
public class Demo5 { public static void main(String[] args) throws IOException { writeText(); readText(); } public static void writeText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "iso8859-1"); outputStreamWriter.write("你好!!!"); outputStreamWriter.close(); } public static void readText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"iso8859-1"); char[] cs = new char[1024]; int length = 0; while((length = inputStreamReader.read(cs))!=-1) { System.out.println(new String(cs,0,length)); } inputStreamReader.close(); } }
在这个之前我实验四种码表(iso8859-1 gbk utf-8 utf-16),发现除了用iso8859-1码表写入记事本,其他的码表编码,记事本都可以解码成功(我们看的懂得文字),至于为什么我不知道可能跟我们系统有关(猜测),我们再来分析为什么会输出这么个鬼东西出来
首先我在存入“你好!!!”的时候,我是使用的iso8859-1码表编码的,注意这里我们编译的是中国文字,在iso8859-1码表中是中国文字是没有对应的码值的,所以会出现乱码,并且每一个字符都是一样的(全是?),也就是对于不能识别的字符都是给的63这个码值(下面例子可以知道),所以导致最后我们根据这个码值进行解码的时候控制台全输出的是?
在上一片中最后一个例子中,跟这个是不同的,那个例子中首先是使用gbk先进行编码成对应的码值,而这个码值在iso8859-1码表中都有对应的字符(iso8859-1码表的特性),最后我们才可以获取最早用gbk编码的后的码值后找到原来的字符,而这个是一开始就是用iso8859-1码表进行的编码,并且iso8859-1码表对于不能识别的字符都是给的63这个码值,这个是解码不出最初的字符的(如果有大神知道可以私信我,谢谢)
public static void readText() throws IOException { File file = new File("D:\\\\新建文件夹\\\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"iso8859-1"); char[] cs = new char[1024]; int length = 0; while((length = inputStreamReader.read(cs))!=-1) { String str = new String(cs,0,length); byte[] buf = str.getBytes("iso8859-1"); System.out.println(Arrays.toString(buf)); } inputStreamReader.close(); }
以上是关于怎么将一个文件流转换成字符串的主要内容,如果未能解决你的问题,请参考以下文章
Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别
转换流:字节输入流转换成字符输入流 InputStreamReader