Create a New folder using Java Program on Windows and Linux machines
new File("/path/to/folder").mkdir();
If you want to created nested folders (i.e. more than one folder on the path may be missing), then use mkdirs()
. See java.io.File
.
Note that forward slashes would normally not work on windows, but Java normalizes the path and translates forward to backward slashes.
Java create directory using File for Unix / Windows
You should use the System
user.home
property which will return the user's home directory in a system independent manner, for example...
File home = new File(System.getProperty("user.home"));
mkdir
will only create the last element in the path, where as mkdirs
will create all the elements that do not exist. Using mkdirs
is probably a slightly better idea as it ensures (where permissions allow) that all elements in the path will be created if they do not exist
How to create a directory in Java?
After ~7 year, I will update it to better approach which is suggested by Bozho.
File theDir = new File("/path/directory");
if (!theDir.exists()){
theDir.mkdirs();
}
JAVA - Create a file in linux server
If the machine "10.30.10.117" is on your local network and if you have the permission to create file on that machine then the problem is
File folder = new File("//10.30.10.117:/home/images/784/");
File file = new File("//10.30.10.117:/home/images/784/1508-1-N.png");
folder.mkdirs();
file.createNewFile();
Before write to file create it.
But if the machine "10.30.10.117" is on external network for your machine then you can't create a file or folder directly from your machine. You need ftp connection vb...
How can I write to a file in linux using java code which runs on my windows machine?
You can do that with external library JSch.
The below should do the job.
JSch jsch = new JSch();
Session session = jsch.getSession("remote_user_name", "remote_host_or_ip", 22); // 22 for SFTP
session.setPassword("remote_password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(10000);
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connection Opened");
ChannelSftp channelSftp = (ChannelSftp) channel;
InputStream inputStream = new FileInputStream("text_file.txt");
channelSftp.put(inputStream, "/remote/folder/file_to_be_rewritten.txt");
System.out.println("File should be uploaded");
channelSftp.disconnect();
session.disconnect();
How to create a file in a directory in java?
The best way to do it is:
String path = "C:" + File.separator + "hello" + File.separator + "hi.txt";
// Use relative path for Unix systems
File f = new File(path);
f.getParentFile().mkdirs();
f.createNewFile();
how to create folders for a specific user on a web application container?
I wouldn't expose the folder directly through URLs as that would be a serious security concern where one user can access the folder of another user if the naming scheme is understood.
Instead, I would
- create folders by user name in a separate location that is not accessible through the URL, using the
java.io.File.mkdir()
. write a simple Servlet that would read the
servletRequest.getPathInfo()
, identifies the user name from the path, along with the file to be served. For this to happen, the servlet class and URL pattern should be defined as follows:<servlet-mapping>
<servlet-name>read-file</servlet-name>
<url-pattern>/read-file/*</url-pattern>
</servlet-mapping>To access a file
a.pdf
of userxyz
, the URL would behttp://server/read-file/xyz/a.pdf
.check if the user accessing the URL is same as the user name found in the path info.
- read the file through a
FileInputStream
in blocks of, say 1MB, and write into theServletOutputStream
(without storing anywhere in between) after setting the appropriate values in theContent-Type
andContent-Disposition
headers.
Creating a folder within web server under /public_html/ in Java
To point to public web directory use
ServletContext context = request.getServletContext();
String path = context.getRealPath("/");
and append your path to the path
(above resolved)
Related Topics
Android Studio Google Jar File Causing Gc Overhead Limit Exceeded Error
Change the System Brightness Programmatically
How to Set Addsnapshotlistener and Remove in Populateviewholder in Recyclerview Item
Java 7 Language Features with Android
The Following Classes Could Not Be Instantiated: - Android.Support.V7.Widget.Toolbar
Illegalblocksizeexception When Trying to Encrypt and Decrypt a String with Aes
How to Get Unique Random Product in Node Firebase
Android: How to Get the Current Day of the Week (Monday, etc...) in the User's Language
How Delete a Collection or Subcollection from Firestore
Foreign Key Constraints in Android Using SQLite? on Delete Cascade
Service Discovery Failed Exception Using Bluetooth on Android
Sqlite in Android How to Update a Specific Row
Understand the R Class in Android
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code