java的file文件读取问题

    技术2022-07-11  81

    先说一下,遇到的难点。就是我想获取到本地的图片,文件路径如:D:/image/1.jpg。 在网上也查了不少资料,但是好像不太好使,怎么说那。现在是获取本地的1.txt文件是可以显示出内容的。但是换成 1.jpg 控制台就显示乱码。因为很少接触到file流,这方面不是太了解。请各位帮忙看看。谢谢啦,谢谢啦,谢谢啦。

    废话不多说,上代码。

    package cn.test.dome; import java.io.*; public class TestDome { public static void main(String[] args) throws IOException { final String path = "D:\\L\\Pictures\\77777/1.jpg"; //1、得到数据文件 File file = new File(path); //2、建立数据通道 FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024]; int length = 0; //循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。 //当文件读取到结尾时返回 -1,循环结束。 while((length = fileInputStream.read(buf)) != -1){ System.out.print(new String(buf,0,length)); } //最后记得,关闭流 fileInputStream.close(); } }

    这是获取图片的显示控制台,展示的乱码。

    package cn.test.dome; import java.io.*; public class TestDome { public static void main(String[] args) throws IOException { final String path = "D:\\L\\Pictures\\77777/1.txt"; //1、得到数据文件 File file = new File(path); //2、建立数据通道 FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024]; int length = 0; //循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。 //当文件读取到结尾时返回 -1,循环结束。 while((length = fileInputStream.read(buf)) != -1){ System.out.print(new String(buf,0,length)); } //最后记得,关闭流 fileInputStream.close(); } }

    这是获取文件的显示控制台,展示的是文件中内容。

    Processed: 0.011, SQL: 9