「java写文件代码」java写文本文件

博主:adminadmin 2023-01-10 21:30:10 666

今天给各位分享java写文件代码的知识,其中也会对java写文本文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎样实现读写TXT文件

主要有用到java原生态的Io类,没有第三个包。直接上代码:

import java.io.*;

public class write {

    public static void main(String[] args) {

        write("E://123.txt", "hello");

    }

    public static void write(String path, String content) {

        try {

            File f = new File(path);

            

            if (f.exists()) {

                System.out.println("文件存在");

            } else {

                System.out.println("文件不存在,正在创建...");

                if (f.createNewFile()) {

                    System.out.println("文件创建成功!");

                } else {

                    System.out.println("文件创建失败!");

                }

            }

            BufferedWriter output = new BufferedWriter(new FileWriter(f));

            output.write(content);

            output.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

急求:JAVA编写复制文件夹的代码

一个简单的方式就是调用cmd命令,使用windows自带的功能来替你完成这个功能

我给你写个例子

import java.io.*;

public class test{

public static void main(String[] args){

BufferedReader in = null;

try{

// 这里你就当作操作对dos一样好了 不过cmd /c 一定不要动

Process pro = Runtime.getRuntime().exec("cmd /c copy d:\\ReadMe.txt e:\\");

in = new BufferedReader(new InputStreamReader(pro.getInputStream()));

String str;

while((str = in.readLine()) != null){

System.out.println(str);

}

}catch(Exception e){

e.printStackTrace();

}finally{

if(in != null){

try{

in.close();

}catch(IOException i){

i.printStackTrace();

}

}

}

}

}

Java中文件下载该怎么写代码求高手指导

if (upfile.exists()) {

bytes = FileUtils.readFileToByteArray(upfile);

response.setContentType("application/x-download");

String agent = request.getHeader("USER-AGENT");//用户代理

// 防止中文文件名乱码

if (null != agent -1 != agent.indexOf("MSIE")) {

String codedfilename = StringUtils.replace(URLEncoder.encode(fileName, "UTF-8"), "+", "%20");

response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);

} else if (null != agent -1 != agent.indexOf("Mozilla")) {

String codedfilename = MimeUtility.encodeText(fileName, "UTF-8", "B");

response.setHeader("Content-Disposition", "attachment;filename=" + codedfilename);

} else {

response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

}

response.setContentLength(bytes.length);

response.getOutputStream().write(bytes);

}

java写文件代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java写文本文件、java写文件代码的信息别忘了在本站进行查找喔。