java - Rename a known file using a list of string from a .txt file -
i'm trying write code lets me rename specific file (with known name , directory) using txt file has inside list of names. specificly, want rename episode using txt file has names of episodes in season.
this code wrote: main class:
import java.io.ioexception; public class main { public static void main(string[ ] args) throws ioexception { string file_name = "c:/users/home/desktop/friends season 2 titles.txt"; try { readfile file = new readfile (file_name); string[] arrlines = file.openfile(); int i; /*for ( i=0; < arrlines.length; i++ ) { // perhaps i'll use loop later on... system.out.println( arrlines ) ; } */ renamefile newfile = new renamefile (); string file2_name = "c:/users/home/desktop/friends_s02e01_720p_bluray_sujaidr.mkv"; newfile.renamesinglefile(arrlines[0], file2_name); } catch (ioexception e) { system.out.println(e.getmessage()); } } }
readfile class:
import java.io.ioexception; import java.io.filereader; import java.io.bufferedreader; public class readfile { private string path; public readfile(string filepath){ path = filepath; } int readlines() throws ioexception { filereader file_to_read = new filereader(path); bufferedreader bf = new bufferedreader(file_to_read); @suppresswarnings("unused") string aline; int numoflines = 0; while ((aline = bf.readline()) != null){ numoflines++; } bf.close(); return numoflines; } public string[] openfile() throws ioexception { filereader fr = new filereader(path); bufferedreader textreader = new bufferedreader(fr); int numberoflines = readlines(); string[] textdata = new string[numberoflines]; (int i=0; < numberoflines; i++) { textdata = textreader.readline(); } textreader.close( ); return textdata; } }
renamefile class:
import java.io.file; public class renamefile { public void renamesinglefile(string file1_path, string file2_path){ file oldname = new file (file1_path); file newname = new file (file2_path); if(oldname.renameto(newname)) { system.out.println("the file has been renamed to: " +newname); } else { system.out.println("the file not renamed."); } } }
for reason, when run program message in console: "the file not renamed.", means there's wrong renamesinglefile method (or parameters file1_path , file2_path of method).
i don't know wrong piece of code...
in renamesinglefile
method should put file2_name
(the title renamed) first parameter, , include directory path , file type when naming:
newfile.renamesinglefile(file2_name, "c:/users/home/desktop/" + arrlines[0] + ".mkv")
Comments
Post a Comment