gpt4 book ai didi

java - java在指定目录动态创建文本文件,写入数据,从该文件读取数据

转载 作者:行者123 更新时间:2023-12-02 02:46:28 25 4
gpt4 key购买 nike

最近我尝试编写上述给定标题的代码,问题是我无法获得足够的日期来查看我的代码。它显示了一些错误,例如

在 tagfile.createnewfile() 之后显示分号无效标记; 让我们看一下代码:

公共(public)类WriteToFileExample {

String path = "G:"+File.separator+"Software"+File.separator+"Files";

String fname= path+File.separator+"fileName.txt";

boolean bool=false;

File f = new File(path);

File f1 = new File(fname);

try {

f1.createNewFile();

bool=f.mkdirs() ;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();
}
}
private static final String FILENAME = "G:\\Software\\Files\\anil.txt";

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

String content = null;

String[] str = new String[100];

try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {

System.out.println(" enter no.of line to be inserted into file");

int k = sc.nextInt();

for (int i = 0; i <= k; i++) {

System.out.println(" enter " + i + " line data");

content = sc.nextLine();

bw.write(content);

str[i] = content;

}

System.out.println("The content present in\n

G:\Software\Files\anil.txt 是:");

        for (int i = 1; i <= k; i++)
System.out.println(str[i]);

} catch (IOException e) {

e.printStackTrace();

}

}

}

最佳答案

请找到代码然后就可以使用

**Writing** : PrintWriter,Scanner,BufferedWriter etc to write to that file
**Reading** :FileReader,BufferReader,Scanner with these readers etc

String path = "G:"+File.separator+"Software"+File.separator+"Files";
String fname= path+File.separator+"fileName.txt";
File f = new File(path);
File f1 = new File(fname);
f.mkdirs() ;
try {
f1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

引用号:https://www.lynda.com/Java-tutorials/Reading-writing-text-files/107061/113497-4.html

引用上面的视频可能会有所帮助

关于java - java在指定目录动态创建文本文件,写入数据,从该文件读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44502274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com