cancel
Showing results for 
Search instead for 
Did you mean: 

Need to Rename particular file in Java.

Former Member
0 Kudos

Hi Experts,

Java, we have a requirement where we need to rename particular file with the string which is inside the file.

String should be inside the file between 42 and 59 characters.

Greatly appreciated if you help .........

Thanks,

Venkat

Accepted Solutions (1)

Accepted Solutions (1)

former_member205363
Contributor
0 Kudos

Hi,

You can try the following code....

File oldFile = new File("C:/check/Edit1.txt");

String fileName = "";

try{

FileReader fr = new FileReader("C:/check/Edit1.txt");

char ch;

int count=1;

do {

ch = (char)fr.read();

count++;

if (ch != -1 && count >= 42)

fileName = fileName.concat(Character.toString(ch));

} while (ch != -1 && count<=59);

System.out.println(fileName);

fr.close();

}catch(IOException e){

System.out.println(e.getMessage());

}

oldFile.renameTo(new File("C:/check/"fileName".txt"));

If you get any issues let me know.........

Regards,

Lakshmi Prasad.

Former Member
0 Kudos

Thank you Lakshmi, My issue is resolved....

Answers (0)