[Gukulkan]

Learn, develop, play.

Full path to file Java

To get path to file in Java, we are using getAbsolutePath from File:


File myFile = File("c:\\first\\second\\3t\\myFile.txt");
System.out.println(myFile.getAbsolutePath());

Get file size in Java

Get size in java we can in bytes, KB, MB etc. you can find using this code:


File myFile=new File("c:\\myFile.txt");
System.out.println("bytes : " + myFile.length());
System.out.println("Kbytes : " + myFile.length() / 1024);
System.out.println("Mbytes : " + (myFile.length() / 1024) / 1024);
\\ etc

Make file writable Java

One post ago we change it to read only, now we make it writable:


File myFile = new File("c:\\myFile.txt");
myFile.setWritable(true);
System.out.println(myFile.canWrite());

Make file Read only Java

Change file to read only in java really simple, just run next code:


File myFile = new File("c:\\myFile.txt");
myFile.setReadOnly();
System.out.println(myFile.canWrite());

Change last modify date in file Java

To change last modify date file in java simple.
Just use setLastModified from File to this:

try{
File myFile = new File("C:\\myFile.txt");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(simpleDateFormat.format(myFile.lastModified()));
String newDateString = "01/01/2001";
Date newDate = simpleDateFormat.parse(newDateString);
myFile.setLastModified(newDate.getTime());
System.out.println(simpleDateFormat.format(myFile.lastModified()));

}catch(ParseException e){
e.printStackTrace();
}

Modified date of file Java

If you want to know last modified date for file in java, run this code:


File myFile = new File("c:\\myFile.txt");

System.out.println(myFile.lastModified());

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

System.out.println(simpleDateFormat.format(myFile.lastModified()));

Time file creation Java

Here we show it for Windows:


try{

Process process =
Runtime.getRuntime().exec("cmd /c dir c:\\file.log /tc");

BufferedReader bufferedReader =
new BufferedReader(
new InputStreamReader(process.getInputStream()));

String data ="";

for(int i=0; i<6; i++){ data = bufferedReader.readLine(); } System.out.println(data); // Here you can parse it like you want }catch(IOException e){ e.printStackTrace(); }

Move files Java

There is 2 variants of file moving:
1. Using renameTo
2. Or just copy and delete

#1

try{

File justfile=new File("C:\\firstFolder\\justfile.txt");

if(justfile.renameTo(new File("C:\\secondFolder\\" + justfile.getName()))){
System.out.println("Successful!");
}else{
System.out.println("Failed!");
}

}catch(Exception e){
e.printStackTrace();
}

#2

InputStream inputStream = null;
OutputStream outputStream = null;

try{

File firstFile =new File("C:\\firstFolde\\file.txt");
File secondFile =new File("C:\\secondFolder\\file.txt");

inputStream = new FileInputStream(firstFile);
outputStream = new FileOutputStream(secondFile);

byte[] buffer = new byte[1024];

int leng;

while ((leng = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, leng);
}

inputStream.close();
outputStream.close();

firstFile.delete();

System.out.println("Successful!");

}catch(IOException e){
e.printStackTrace();
}

Copy files Java

For copy one file to another one, just run next code:


InputStream inStream = null;
OutputStream outStream = null;

try{

File firstfile =new File("firstfile.png");
File secondfile =new File("secondfile.txt");

inStream = new FileInputStream(firstfile);
outStream = new FileOutputStream(secondfile);

byte[] buffer = new byte[1024];

int length;

while ((length = inStream.read(buffer)) > 0){

outStream.write(buffer, 0, length);

}

inStream.close();
outStream.close();

}catch(IOException e){
e.printStackTrace();
}

Java JMX Authentication and Authorization

Here is shot example about Authentication and Authorization in JMX Java code

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Tester {

public static void main(String[] args) {

String url = "service:jmx:rmi:///jndi/rmi://localhost:19999/jmxrmi"; // Link to connect
JMXServiceURL serviceUrl;
JMXConnector jmxConnector;
try {
serviceUrl = new JMXServiceURL(url);
Map env = new HashMap<>();
String[] credentials = {"controlRole", "tomcat"}; // Login and password
env.put(JMXConnector.CREDENTIALS, credentials);
jmxConnector = JMXConnectorFactory.connect(serviceUrl, env);
MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
ObjectName name = new ObjectName("java.lang:type=OperatingSystem");// MBean which we using

System.out.println(mbeanConn.getAttribute(name, "ProcessCpuTime"));// Attribute

} catch (IOException e) {

e.printStackTrace();
} catch (MalformedObjectNameException e) {

e.printStackTrace();
} catch (AttributeNotFoundException e) {

e.printStackTrace();
} catch (InstanceNotFoundException e) {

e.printStackTrace();
} catch (MBeanException e) {

e.printStackTrace();
} catch (ReflectionException e) {

e.printStackTrace();

}
}

}