「java怎么写文件」java中写文件

博主:adminadmin 2022-11-22 16:01:06 101

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

本文目录一览:

java如何写入文件

package filewriter;  

  

import java.io.FileWriter;  

import java.io.IOException;  

  

public class IOExceptionDemo {  

  

    private static final String LINE_SEPARATOR = System.getProperty("line.separator");  

    public static void main(String[] args) {  

  

        FileWriter fw = null;  

        try {  

            fw = new FileWriter("k:\\Demo.txt", true);  

            fw.write("hello" + LINE_SEPARATOR + "world!");  

        } catch (Exception e) {  

            System.out.println(e.toString());  

        } finally {  

            if (fw != null)  

                try {  

                    fw.close();  

                } catch (IOException e) {  

                    throw new RuntimeException("关闭失败!");  

                }  

        }  

    }  

}

Java文件读写

实用的模糊(通配符)文件查找程序

1 import java.io.File;

2 import java.util.regex.Matcher;

3 import java.util.regex.Pattern;

4 import java.util.ArrayList;

5

6 /** *//**

7 * pTitle: FileService /p 8* pDescription: 获取文件 /p 9* pCopyright: Copyright (c) 2007/p

10* pCompany: /p

11* @author not attributable

12* @version 1.0

13*/

14public class FileService {

15 public FileService() {

16 }

17

18 /** *//**

19 * 在本文件夹下查找

20 * @param s String 文件名

21 * @return File[] 找到的文件

22 */

23 public static File[] getFiles(String s)

24 {

25 return getFiles("./",s);

26 }

27

28 /** *//**

29 * 获取文件

30 * 可以根据正则表达式查找

31 * @param dir String 文件夹名称

32 * @param s String 查找文件名,可带*.?进行模糊查询

33 * @return File[] 找到的文件

34 */

35 public static File[] getFiles(String dir,String s) {

36 //开始的文件夹

37 File file = new File(dir);

38

39 s = s.replace('.', '#');

40 s = s.replaceAll("#", "\\\\.");

java如何创建文件?

public void createFile(){\x0d\x0a\x0d\x0a//path表示你所创建文件的路径\x0d\x0aString path = "d:/tr/rt";\x0d\x0aFile f = new File(path);\x0d\x0aif(!f.exists()){\x0d\x0a f.mkdirs();\x0d\x0a} \x0d\x0a// fileName表示你创建的文件名;为txt类型;\x0d\x0aString fileName="test.txt";\x0d\x0aFile file = new File(f,fileName);\x0d\x0aif(!file.exists()){\x0d\x0atry {\x0d\x0afile.createNewFile();\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a//现在你可以在d:/tr/rt 目录下找到test.txt文件

关于java怎么写文件和java中写文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。