将文件保存到Java中的特定目录? [英] Saving files to a specific directory in Java?

查看:47
本文介绍了将文件保存到Java中的特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我对Java还是很陌生,我无法弄清楚.
基本上,我试图从网站上下载一些文件,我想将它们保存到特定的文件夹(而不是Java文件所在的文件夹的默认文件夹).我该怎么办?

This is probably a silly question, but I'm pretty new to Java and I can't figure it out.
Basically, I'm trying to download some files from a website and I want to save them to a particular folder (rather than the default of the same folder that my Java file is located in). How can I do this?

我一直在使用 FileReader BufferedReader BufferedInputStream FileOutputStream类.

谢谢:)

推荐答案

Java与IO非常友好.尝试这样的事情:

Java is pretty friendly with IO. Try something like this:

File file = new File("/some/absolute/path/myfile.ext");
OutputStream out = new FileOutputStream(file);
// Write your data
out.close();

注意:

  • 您的程序需要权限才能写入目录.
  • 如果您的路径字符串的第一个字符不是/,它将相对于您的当前"目录
  • 如果要编写文本,可能会发现 BufferedWriter 更容易: BufferedWriter writer = new BufferedWriter(new FileWriter(file)); .它具有 newLine() write(String)方法
  • Your program needs permission to write to the directory.
  • If the first character of your path string is not /, it will be relative to your "current" directory
  • If you're writing text, you might find a BufferedWriter easier: BufferedWriter writer = new BufferedWriter(new FileWriter(file));. It has newLine() and write(String) methods

这篇关于将文件保存到Java中的特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆