首页 > 编程知识 正文

java如何用io流保存,java的io流总结

时间:2023-12-28 21:10:48 阅读:328682 作者:XYBQ

本文目录一览:

java如何实现 io流传输过来的文件,提示另存为弹出窗口?

弹出窗口,我理解为浏览器弹出窗口,所以必定有后端服务器程序,这里重点说的就是服务器程序。

第一步:设置Response头部(最关键)

response.setContentType("application/octet-stream;charset=UTF-8");

// 设置弹出框提示的文件名

response.addHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));

第二步:解析输入流

// 这里的in为你的输入流

BufferedInputStream is = new BufferedInputStream(in);

// 准备缓冲区

byte[] buffer = new byte[4096];

第三步:将输入流转换为输出流

BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());

int offset = 0;

while((offset = is.read(buffer, 0, 4096) -1) {

os.write(buffer, 0, offset)

}

第四步:关闭输入输出流

os.close();

is.close();

JAVA怎么用IO流保存一部电影

其实原理就是下载电影文件内容至一个IO流中,流最终是可以保存为文件的。

JAVA中我想用IO流把服务器上的文件保存到本机

理论上讲这是不行的!除非你手动进行远程下载,因为服务器不能自动操作客户机,这样做无异于病毒。

java的IO流中可以通过自定义一个数组来存储数据,为什么还要使用缓存区呢?

IO流自定义字节流的缓冲区:

思路:BufferedInputStream类中read()方法的工作原理

1)先一个一个从字节流中读取字节,读取一定量(自定义)之后,存储在一个字节数组(缓冲区)(FileInputStream.read(byte[] b)),并获得存储数量(read方法的返回值)。

2)一个一个字节返回,返回一个,存储数量减1,然后指针往后移一位,准备取下一个。

3)如果存储数量为0 ,代表当前数组中所有数据已经全部取完,此时再来一次读取(read(byte[] b)),再获得此次存储数量。

4)如果存储数量(即read方法返回-1),代表读到文件末尾,返回-1。

因此,需要用到以下几个变量:

读取的字节数量,指向数组中准备取哪一个的指针,将要返回的字节变量。

java中如何通过IO流将稀疏数组写入磁盘和从磁盘中读取,整行存,整行取

//写入磁盘

public static void writ(int sparseArr[][]) {

System.out.println("写入磁盘的数据中~~~~~~");

File file = new File("E:\java\sparseArr.txt");

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter(file));

if (!file.exists()) {

file.createNewFile();

}

StringBuilder allBuilder = new StringBuilder();

for (int[] rows : sparseArr) {

StringBuilder rowBuilder = new StringBuilder();

for (int item : rows) {

rowBuilder.append(item + "t");

}

allBuilder.append(rowBuilder + "n");

}

bw.write(String.valueOf(allBuilder));

bw.flush();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//读取磁盘

public static int[][] read() {

System.out.println("读取磁盘的数据中~~~~~~");

File file = new File("E:\java\sparseArr.txt");

int[][] sparseArr = null;

try {

BufferedReader br = new BufferedReader(new FileReader(file));

if (!file.exists()) {

file.createNewFile();

}

String row = br.readLine();

String[] s = new String(row).split("t");

sparseArr = new int[Integer.parseInt(s[0])][Integer.parseInt(s[1])];

while ((row = br.readLine()) != null) {

String[] s2 = new String(row).split("t");

sparseArr[Integer.parseInt(s2[0])][Integer.parseInt(s2[1])] = Integer.parseInt(s2[2]);

}

br.close();

} catch (IOException e) {

e.printStackTrace();

}

return sparseArr;

}

java怎么使用io流读取一个文件夹里面

使用工具类Properties

工具类有Load()方法 用于加载文件

首先将文件写成流(输入)

File file=new  File(confPath);

            in = new FileInputStream(file);

加载流load(in)如果需要操作则完成具体操作

输出流并保存文件

out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

            cp.store(out2);

完成

具体实例代码

public String updateConfParam(ConfParam cpl) throws IOException {

        String error = null;

        Properties cp=new Properties();

        InputStream in= null;

        OutputStreamWriter out1=null;

        OutputStreamWriter out2=null;

        JSONObject jObj = new JSONObject();

        try {

            String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName());

            File file=new  File(confPath);

            in = new FileInputStream(file);   

            cp.load(in);

            out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK");

            cp.store(out1);

            cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()));

            cp.setProperty(cpl.getParaKey(), cpl.getParaValue());    

            out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

            cp.store(out2);

            jObj.put("paraOldValue", cpl.getParaOldValue());

            jObj.put("paraValue", cpl.getParaValue());

        } catch (FileNotFoundException e) {

            error=e.getMessage();

        } catch (IOException e1) {

            error = e1.getMessage();

        }

        finally{

            if(in !=null){

                in.close();

            }

            if(out1 !=null){

                out1.close();

            }

            if(out2 !=null){

                out2.close();

            }

            if(error != null){

                jObj.put("error", error);

            }

        }

        return jObj.toString();

    }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。