A story about trying to operate JAVA File

JavaTest.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class JavaTest {
	public static void main(String args[]) {
		String initPath="C:/test1/test2/test3/sample2.txt";
		String[] patharray=initPath.split("/");
		String fileName=patharray[patharray.length-1];
		String dirName="";
		for(String item:patharray) {
			if(item.equals(patharray[0])) continue;
			dirName=dirName+"/"+item;
			System.out.println(dirName);
			File file = new File(dirName);
			file.mkdir();
		}
		
		String path=dirName;
		String path2=fileName;
		File file = new File(path+"/"+path2);
		try {
			if (file.createNewFile()){
				System.out.println("success");
			}else{
				System.out.println("Failure");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			FileWriter writer = new FileWriter(path+path2);
			writer.write("testtesttest");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

Yes. It will make an initPath guy. However, there is a relative of the mkdir method called the mkdirs method.

JavaTest.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class JavaTest {
	public static void main(String args[]) {
		String initPath="C:/test1/test2/test3/sample3.txt";
		String[] patharray=initPath.split("/");
		String fileName=patharray[patharray.length-1];
		String dirName="";
		for(String item:patharray) {
			if(item.equals(patharray[0])) continue;
			if(item.equals(patharray[patharray.length-1])) continue;
			dirName=dirName+"/"+item;
		}
		
		File filemake = new File(dirName);
		filemake.mkdirs();
		String path=dirName;
		String path2=fileName;
		File file = new File(path+"/"+path2);
		try {
			if (file.createNewFile()){
				System.out.println("success");
			}else{
				System.out.println("Failure");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			FileWriter writer = new FileWriter(path+path2);
			writer.write("testtesttest");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

It was quite a while after I noticed this. In the first place, if you have this, I feel that you don't have to turn it around with for. Also, if it's a memory, it seems that there was something that created a directory while writing ... Reference URL https://www.sejuku.net/blog/20527

Recommended Posts

A story about trying to operate JAVA File
A story about trying to get along with Mockito
A story about trying hard to decompile JAR files
A story about misunderstanding how to use java scanner (memo)
A story about Java 11 support for Web services
A story about the JDK in the Java 11 era
The story of forgetting to close a file in Java and failing
How to jump from Eclipse Java to a SQL file
A story about reducing memory consumption to 1/100 with find_in_batches
How to convert a file to a byte array in Java
A story about developing ROS called rosjava with java
A note about Java GC
[Java] Create a temporary file
A story about PKIX path building failed when trying to deploy to tomcat with Jenkins
A story about going to a Docker + k8s study session [JAZUG Women's Club x Java Women's Club]
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
About the behavior when doing a file map with java
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Upload a file using Java HttpURLConnection
Run a batch file from Java
[Java] How to create a folder
Log output to file in Java
About file copy processing in Java
How to make a Java array
A story that I struggled to challenge a competition professional with Java
A funny story stuck in a mess when trying to import fx-clj
A story of frustration trying to create a penetration environment on Ubuntu 20.04
[Note] A story about changing Java build tools with VS Code
How to automatically operate a screen created in Java on Windows
A story about hitting the League Of Legends API with JAVA
A story about having a hard time aligning a testing framework with Java 6
A story about making a calculator to calculate the shell mound rate
[Xcode] How to add a README.md file
About returning a reference in a Java Getter
How to make a Java calendar Summary
A story about changing jobs from a Christian minister (apprentice) to a web engineer
[Java] How to use the File class
[IntelliJ IDEA] How to automatically add final when saving a Java file
A story addicted to JDBC Template placeholders
About the procedure for java to work
To create a Zip file while grouping database search results in Java
[Creating] A memorandum about coding in Java
[Introduction to Java] How to write a Java program
[Beginner] A story about starting studying Java for job hunting ~ 2nd month ~
How to make a Discord bot (Java)
[Beginner] A story about starting studying Java for job hunting ~ 3rd month ~
Why does Java call a file a class?
[Beginner] A story about starting studying Java for job hunting ~ 1st month ~
How to record JFR (Java Flight Recorder) and output a dump file
How to find out the Java version of a compiled class file
[Java small story] Monitor when a value is added to the List
Hanashi stumbled a little on path trying to study Java with VScode
How to print a Java Word document
A story about converting character codes from UTF-8 to Shift-jis in Ruby
A story that was embarrassing to give anison file to the production environment
[Beginner] A story about starting studying Java for job hunting ~ 5th month ~
Volume of trying to create a Java Web application on Windows Server 2016
Sample to unzip gz file in Java
A story about sending a pull request to MinGW to update the libgr version
[Beginner] A story about starting studying Java for job hunting ~ 4th month ~