Last active
November 25, 2021 01:27
-
-
Save XDo0/9b8a682ab5d3035c0608f5477c6966f4 to your computer and use it in GitHub Desktop.
Java File Operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
File temp = File.createTempFile("tmp", ".txt"); | |
System.out.println("File Path: "+temp.getAbsolutePath()); | |
Date filetime = new Date(temp.lastModified()); | |
System.out.println(filetime.toString()); | |
System.out.println(fileToChange.setLastModified(System.currentTimeMillis())); | |
temp.deleteOnExit(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
public class RunoobTest { | |
public static void main(String[] args) throws IOException { | |
File oldName = new File("./test.txt"); | |
File newName = new File("./test-2.txt"); | |
if (newName.exists()) { | |
throw new java.io.IOException("file exists"); | |
} | |
if(oldName.renameTo(newName)) { | |
System.out.println("rename done"); | |
} else { | |
System.out.println("Error"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Main { | |
public static void main(String[] args) { | |
//write | |
try { | |
BufferedWriter out = new BufferedWriter(new FileWriter("srcfile")); | |
out.write("123"); | |
out.close(); | |
} catch (IOException e) { | |
} | |
//copy | |
InputStream in = new FileInputStream(new File("srcfile")); | |
OutputStream out = new FileOutputStream | |
(new File("destnfile")); | |
byte[] buf = new byte[1024]; | |
int len; | |
while ((len = in.read(buf)) > 0) { //in.read() is important | |
out.write(buf, 0, len); | |
} | |
in.close(); | |
out.close(); | |
//read | |
try { | |
BufferedReader in = new BufferedReader(new FileReader("destnfile")); | |
String str; | |
while ((str = in.readLine()) != null) { | |
System.out.println(str); | |
} | |
System.out.println(str); | |
} catch (IOException e) { | |
} | |
//delete | |
try{ | |
File file = new File("c:\\test.txt"); | |
if(file.delete()){ | |
System.out.println(file.getName() + " has been deleted!"); | |
}else{ | |
System.out.println("delete failed!"); | |
} | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
public static long getFileSize(String filename) { | |
File file = new File(filename); | |
if (!file.exists() || !file.isFile()) { | |
System.out.println("File is not exist!"); | |
return -1; | |
} | |
System.out.println(file.setReadOnly()); | |
System.out.println(file.canWrite()); | |
return file.length(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment