创建文件并写入数据



public class IO {
    //生成文件路径
    private static String path = "D:/";
    
    //文件路径+名称
    private static String filenameTemp;
    /**
     * 创建文件
     * @param fileName  文件名称
     * @param filecontent   文件内容
     * @return  是否创建成功,成功则返回true
     */
    public static boolean createFile(String fileName,String filecontent){
        Boolean bool = false;
        filenameTemp = path+fileName+".txt";//文件路径+名称+文件类型
        File file = new File(filenameTemp);
        try {
            //如果文件不存在,则创建新的文件
            if(!file.exists()){
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is "+filenameTemp);
                //创建文件成功后,写入内容到文件里
                writeFileContent(filenameTemp, filecontent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return bool;
    }
    
    /**
     * 向文件中写入内容
     * @param filepath 文件路径与名称
     * @param newstr  写入的内容
     * @return
     * @throws IOException
     */
    public static boolean writeFileContent(String filepath,String newstr) throws IOException{
        Boolean bool = false;
        String filein = newstr+"\r\n";//新写入的行,换行
        String temp  = "";
        
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos  = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            
            //文件原有内容
            for(int i=0;(temp =br.readLine())!=null;i++){
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(filein);
            
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }
    
    /**
     * 删除文件
     * @param fileName 文件名称
     * @return
     */
    public static boolean delFile(String fileName){
        Boolean bool = false;
        filenameTemp = path+fileName+".txt";
        File file  = new File(filenameTemp);
        try {
            if(file.exists()){
                file.delete();
                bool = true;
            }
        } catch (Exception e) {
        }
        return bool;
    }
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        createFile(uuid+"myfile", "这是我的测试");
    }
    
    
    
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python创建txt文件写入数据的代码如下: ``` with open("example.txt", "w") as file: file.write("Hello World") ``` 其中 `"example.txt"` 是文件名, `"w"` 表示以写入模式打开文件。 使用 `file.write("Hello World")` 将字符串 "Hello World" 写入文件。 ### 回答2: Python是一种高级编程语言,不仅具有简洁易懂的语法,而且在数据处理方面表现出色。其中,创建并写入txt文件是Python数据处理中的常见需求。下面,我将详细介绍Python如何创建txt文件写入数据。 步骤: 1. 打开文件 首先,需要指定创建文件的路径和文件名。为了创建文件,可以使用Python中的open()函数。open()函数需要两个参数,第一个参数是文件路径和文件名,第二个参数是打开文件的模式。以写入模式w打开文件,并指定文件路径和文件名为demo.txt。 file = open("demo.txt", "w") 2. 写入数据 创建完成文件后,需要向其中写入数据。可以使用Python的write()函数向文件写入文本。下面示例向demo.txt文件写入了一行文本。 file.write("这是一个demo文件。\n") 3. 关闭文件 写入数据后,必须关闭文件,释放系统资源。可以使用Python中的close()函数来关闭文件。 file.close() 完整的代码如下: # 打开文件 file = open("demo.txt", "w") # 写入数据 file.write("这是一个demo文件。\n") # 关闭文件 file.close() 总结: 以上就是Python创建txt文件写入数据的详细步骤。通过这个简单的例子,我们可以看到Python的处理txt文件非常简单。当然,Python还提供了其他的操作文件的函数和方法,如读取文件、删除文件等等,可以根据实际需要进行学习和尝试。 ### 回答3: Python是一种高级编程语言,是一种解释型语言,具有易学易用的特点。对于文本文件的操作,Python提供了很多种实现方法。这里我将介绍如何使用Python创建txt文件写入数据。 首先,我们需要使用Python内置的open()函数创建或打开一个文件。open()函数需要两个参数,第一个参数为文件名,第二个参数为打开文件的模式。模式分为读模式、写模式、追加模式等。我们在创建和写入文件时,需要使用写模式。 示例代码: ```python file = open("test.txt", "w") ``` 以上代码创建了一个名为test.txt的文件,并以写模式打开它。如果test.txt文件不存在,Python将会在程序运行时自动创建它。如果文件存在,Python将会清空文件内容并重新写入数据。 接下来,我们可以使用write()函数将数据写入文件中。写入数据需要使用字符串格式。写入数据之后,我们需要使用close()函数关闭文件。 示例代码: ```python file = open("test.txt", "w") file.write("Hello World!") file.close() ``` 以上代码创建了一个名为test.txt的文件,并将字符串“Hello World!”写入文件中。最后调用close()函数关闭文件。 如果我们需要向文件写入多行数据,可以使用Python的多行字符串,例如三重引号 """ 或者 ''' 标识。 示例代码: ```python file = open("test.txt", "w") file.write("""第一行数据 第二行数据 第三行数据""") file.close() ``` 以上代码将“第一行数据”、“第二行数据”和“第三行数据”分别写入一个新文件test.txt中。 在写文件时,我们可以使用with语句简化代码。with语句可以自动管理文件的打开和关闭,可以保证打开文件后一定会关闭文件。 示例代码: ```python with open("test.txt", "w") as file: file.write("Hello World!") ``` 以上代码使用了with语句,自动创建文件写入数据。with语句在代码执行完毕后会自动关闭文件。 以上就是Python创建txt文件写入数据的基本操作方法。通过这些方法,我们可以方便地向文件写入各种数据信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值