如何将信息从文件夹传输到文件?
How do I transfer information from a folder to file?
所以我正在研究这个有助于为我建立网站的项目,我希望从文件夹(包含文件)中读取数据并写入 html 文档。我做了一些研究,但我总是没有打印任何内容或出现错误。让我知道是否有办法做到这一点?这是我的项目,我已将文件夹位置和输出更改为 .txt
public class Project {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter("output.txt");
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
out.print(filePath);
}
});
}
}
确保使用 PrintWriter("output.txt", true) 自动刷新或在程序结束时刷新它。
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
File dir = new File("images");
String[] list = dir.list();
if (list != null) {
for (String f : list) {
String[] fileName = f.split("\.");
if (fileName.length > 1 && fileName[1].equals("png")) {
// System.out.println(f);
out.println(f);
}
}
} else {
System.out.println("list returned null because dir.canRead is " + dir.canRead());
}
}
我在文件 class 中重写了一部分。如果这些需求有任何变化,请告诉我。希望这有帮助。
@JoetheDailyProgrammer,你可能写错了文件。试试这个代码:
public static void main(String[] args) throws IOException {
File out = new File("output.txt");
System.out.println(out.getAbsolutePath());
}
运行 它,它会显示你 "output.txt"
的绝对路径。如果它指向与您预期不同的位置,则在您的应用中使用绝对路径或使用用户主目录,如下所示:
public static void main(String[] args) throws IOException {
File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt"); // file into users homedir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
PrintWriter out = new PrintWriter(outFile); // writer over that file
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
out.print(filePath);
}
});
out.close();
}
}
更新
如果 你真的 打算将文件保存到桌面文件夹中,那么你可能应该按照以下答案导航它:
所以:
public static void main(String[] args) throws IOException {
FileSystemView fsv = FileSystemView.getFileSystemView();
File outFile = new File(fsv.getHomeDirectory() + "/output.txt"); // file into desktop dir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
...
}
所以我正在研究这个有助于为我建立网站的项目,我希望从文件夹(包含文件)中读取数据并写入 html 文档。我做了一些研究,但我总是没有打印任何内容或出现错误。让我知道是否有办法做到这一点?这是我的项目,我已将文件夹位置和输出更改为 .txt
public class Project {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter("output.txt");
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
out.print(filePath);
}
});
}
}
确保使用 PrintWriter("output.txt", true) 自动刷新或在程序结束时刷新它。
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
File dir = new File("images");
String[] list = dir.list();
if (list != null) {
for (String f : list) {
String[] fileName = f.split("\.");
if (fileName.length > 1 && fileName[1].equals("png")) {
// System.out.println(f);
out.println(f);
}
}
} else {
System.out.println("list returned null because dir.canRead is " + dir.canRead());
}
}
我在文件 class 中重写了一部分。如果这些需求有任何变化,请告诉我。希望这有帮助。
@JoetheDailyProgrammer,你可能写错了文件。试试这个代码:
public static void main(String[] args) throws IOException {
File out = new File("output.txt");
System.out.println(out.getAbsolutePath());
}
运行 它,它会显示你 "output.txt"
的绝对路径。如果它指向与您预期不同的位置,则在您的应用中使用绝对路径或使用用户主目录,如下所示:
public static void main(String[] args) throws IOException {
File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt"); // file into users homedir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
PrintWriter out = new PrintWriter(outFile); // writer over that file
Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
out.print(filePath);
}
});
out.close();
}
}
更新
如果 你真的 打算将文件保存到桌面文件夹中,那么你可能应该按照以下答案导航它:
所以:
public static void main(String[] args) throws IOException {
FileSystemView fsv = FileSystemView.getFileSystemView();
File outFile = new File(fsv.getHomeDirectory() + "/output.txt"); // file into desktop dir
System.out.println(outFile.getAbsolutePath()); // print it's location in console
...
}