Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- ArrayList<Integer> al1 = new ArrayList<>();
- ArrayList<Integer> al2 = new ArrayList<>();
- List<Integer> al3 = new ArrayList<>();
- String filename = "";
- int somenum = 5;
- int a = 0;
- int zad3 = 0;
- int choice = 0;
- Scanner sc = new Scanner(System.in);
- String str;
- System.out.println("Введите путь:");
- if (sc.hasNextLine()) {
- filename = sc.nextLine();
- } else {
- System.out.println("Введено неверное значение");
- }
- System.out.println("Записать значения в перпвый массив или второй?");
- if (sc.hasNextInt())
- choice = sc.nextInt();
- else
- System.out.println("введено неверное значение");
- File f = new File(filename);
- StringBuilder sb = new StringBuilder();
- if (f.exists()) {
- try {
- try (BufferedReader br = new BufferedReader(new FileReader(f.getAbsoluteFile()))) {
- String s;
- while ((s = br.readLine()) != null) {
- String[] strs = s.split(" ");
- for (String str1 : strs) {
- str = str1;
- al3.add(Integer.valueOf(str));
- }
- }
- }
- } catch (IOException e) {
- throw new RuntimeException();
- }
- }
- if (choice == 1) {
- for (int i = 0; i < al3.size(); i++) {
- if (al3.get(i) == 1) {
- al1.add(i, al3.get(i));
- }
- }
- for (int i = 0; i < al3.size(); i++) {
- if (al3.get(i) == 0) {
- al1.add(i, al3.get(i));
- }
- }
- }
- if (choice == 2) {
- for (int i = 0; i < al1.size(); i++) {
- if (al1.get(i) == 1) {
- al2.add(i, al1.get(i));
- }
- }
- for (int i = 0; i < al3.size(); i++) {
- if (al3.get(i) == 0) {
- al2.add(i, al3.get(i));
- }
- }
- }
- try {
- if (!f.exists()) f.createNewFile();
- try (PrintWriter pw = new PrintWriter(f.getAbsoluteFile())) {
- if (choice == 1) {
- pw.print(al1);
- }
- if (choice == 2) {
- pw.print(al2);
- }
- }
- } catch (IOException e) {
- throw new RuntimeException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement