-1
import java.io.*;

class Main {
public static void main(String[] args) {
try {
    File file = new File("1.txt");
  InputStreamReader inputStreamReader = new InputStreamReader(System.in); 
  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  

    FileWriter fileReader = new FileWriter(file);
    BufferedWriter bufferedWriter = new BufferedWriter(fileReader); 

    String line;

    while(!(line = bufferedReader.readLine()).equals("exit")) {
        if (!line.equals(null)) {
            line.replace("", " ");
        bufferedWriter.write(line);
        }
    }

    bufferedReader.close(); 
    bufferedWriter.close();
 }  catch (Exception e) {
    e.printStackTrace();
}

} }

4

2 ответа 2

2

Ваш код рабочий!! Просто замените строку System.out.println("Input exception"); на строку exception.printStackTrace();

Вы увидите сообщение

java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at Test.main(Test.java:9)

которая говорит о том, что file.txt не существует

структура должна быть такая (file.txt лежит в одной папке с папкой src)

введите сюда описание изображения

после выполнения кода

введите сюда описание изображения

5
  • Так я же создаю file.txt в папке src
    – Rich
    10 дек 2018 в 12:52
  • файл должен быть не в src, а в одной папке с src !! 10 дек 2018 в 12:53
  • Всё, спасибо, помогло. Ещё 1 вопрос: А как сделать так, чтобы у меня не было пустых строк. Тип, чтобы в процессе копирование их удалить?
    – Rich
    10 дек 2018 в 12:57
  • добавь ифчик в цикле. пугугли с каким условием 10 дек 2018 в 13:01
  • и отметь плиз ответ, как правильный 10 дек 2018 в 13:01
-1
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;

public class Main {

public static void main(String[] args) {

    try(FileInputStream inputStream = new FileInputStream("file.txt")) {
        File directory = new File("src/test");
        if (!directory.exists()) directory.mkdir();

     try (FileOutputStream outputStream = new FileOutputStream("src/test/file.txt")){
             byte[] bytes = new byte[inputStream.available()];
             int length;

             while ((length = inputStream.read(bytes)) != -1){
                 outputStream.write(bytes, 0, length);
            }
        } 
         catch (IOException exception) {
            System.out.println("Output exception");
        }
            }
             catch (IOException exception){
             System.out.println("Input exception");
   } 
}

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.