Good day. Now there is an array of source folders:

Path[] paths = new Path[4]; paths[0] = Paths.get("\\\\komp\\D$\\logs"); paths[1] = Paths.get("\\\\komp\\D$\\logs2"); paths[2] = Paths.get("\\\\komp\\D$\\logs3"); paths[3] = Paths.get("\\\\komp\\D$\\logs4"); 

and one recipient folder:

 Path target = Paths.get("D:\\MyLogs\\"); 

There is a file Setting.txt, where these paths are contained line by line:

 \\\\komp\\D$\\logs \\\\komp\\D$\\logs2 \\\\komp\\D$\\logs3 \\\\komp\\D$\\logs4 D:\\MyLogs 

Please show how to read these lines from a file and insert it into my code, so that there would be something like:

 Path[] paths = new Path[4]; paths[0] = Paths.get("Первая строка из файла Setting.txt"); paths[1] = Paths.get("Вторая строка из файла Setting.txt"); paths[2] = Paths.get("Третья строка из файла Setting.txt"); paths[3] = Paths.get("Четвёртая строка из файла Setting.txt"); 

and one recipient folder:

 Path target = Paths.get("Пятая строка из файла Setting.txt"); 
  • It is necessary to read one line from the file, then repeat five times. Can you explain what exactly you can not? - default locale
  • It's simple - I do not know how to do it. - Alex
  • Understandably, just a few questions here with each of which is easier to understand separately. - default locale

2 answers 2

You just need to read the file line by line. Here's how to implement it:

 // метод возвращает строки из файла в массиве String[] // fileName - имя файла // linesAmount - кол-во. строк, которые необходимо считать из файла (в вашем случае - 5) public String[] getLinesFromFile(String fileName, int linesAmount) { String[] lines = new String[linesAmount]; try (BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(fileName), Charset.forName("КОДИРОВКА_ВАШЕГО_ФАЙЛА")))) { String line; int i = 0; while ((line = reader.readLine()) != null && i < linesAmount) { lines[i++] = line; } } catch (IOException e) { e.printStackTrace(); } return lines; } 

Now in your code:

 String[] lines = getLinesFromFile("Setting.txt", 5); Path[] paths = new Path[4]; paths[0] = Paths.get(lines[0]); paths[1] = Paths.get(lines[1]); paths[2] = Paths.get(lines[2]); paths[3] = Paths.get(lines[3]); Path target = Paths.get(lines[4]); 
  • All the time, he swears at the line: FileInputStream (fileName), To be honest, I myself don’t understand what the FileInputStream is, but the idea asks to close it. - Alex
  • @Alex, about FileInputStream is a file input stream. I / O streams are a separate topic, which I think is best dealt with separately. However, the code is working, just tested. I think the problem is in the Language level of your project in IDEA. If you describe the error in more detail, I can give you a solution. - iGreetYou
  • Simply new FileInputStream (fileName) is underlined in red, it is written on hover: Clouse this "FileInputStream". - Alex
  • @Alex, look in IDEA -> File -> Project Structure -> Project -> Project language level. You must have a value of at least 7. - iGreetYou
  • Specified: SDK default (8 - Lambdas, type annotations etc.) - Alex

I understand you need to read and write them to an array? Here is the code: where value is the line number; rail is the path to the file; MaxInt is the maximum
the number of read lines; everything checked, works!

|| ------------------------------------------------ --- ||

 import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class НовыйClass { public static String rail = "C://Setting.txt"; public static int MaxInt = 6; public static String ReadLocal (int value) throws FileNotFoundException, IOException{ File Setting = new File(rail); FileReader reader = new FileReader(Setting); BufferedReader br = new BufferedReader(reader); String b = br.readLine(); byte i = 0; while (i < value-1) { b = br.readLine(); i++; if (i-1 == value-1) { return b; } } br.close(); reader.close(); return b; } //и второй отрывок кода,будет помещать прочтенное в массив. public static void AddArray() throws IOException{ String[] OutPut = new String[MaxInt]; for (int i = 0; i < MaxInt; i++) { OutPut[i] = ReadLocal(i); } System.out.println(OutPut[1]+ "\n" + OutPut[2]+ "\n" + OutPut[3]+ "\n" + OutPut[4]+ "\n" + OutPut[5]+ "\n"); } } ' 

|| ------------------------------------------------ --- || good luck

  • It helped, thanks a lot. - Alex
  • you're welcome! contact - AseTry