Last active
January 29, 2016 13:11
-
-
Save Viacheslav77/6870a484a985ff6257c5 to your computer and use it in GitHub Desktop.
Написать программу, которая считает текстовый файл, заменит в нем все слова “Hello” на “1234” и запишет изменения в тот-же файл.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| /* Написать программу, которая считает текстовый файл, заменит | |
| * в нем все слова “Hello” на “1234” и запишет изменения в тот-же файл.*/ | |
| public class FileRW { | |
| public static String readText (String pth) throws IOException{ | |
| System.out.println("------------------------------------"); | |
| System.out.println("Читаем Текст из файла...." + pth); | |
| System.out.println("------------------------------------"); | |
| byte[] str; | |
| try(FileInputStream inFile = new FileInputStream(pth)) { | |
| str = new byte[inFile.available()]; | |
| inFile.read(str); | |
| } | |
| String text = new String(str); | |
| System.out.println(text); | |
| System.out.println("------------------------------------"); | |
| return text; | |
| } | |
| public static void writeText (String pth, String text) throws IOException{ | |
| System.out.println("------------------------------------"); | |
| System.out.println("Записываем в файл..." + pth); | |
| System.out.println("------------------------------------"); | |
| byte[] str; | |
| try(FileOutputStream inFile = new FileOutputStream(pth)) { | |
| str = text.getBytes(); | |
| inFile.write(str, 0, str.length); | |
| } | |
| } | |
| public static void main (String[] args) throws IOException { | |
| String text = readText("d:\\1\\n1.txt"); | |
| System.out.println("Исправляем Hello на 1234."); | |
| String new_text = text.replace ("Hello", "1234"); | |
| writeText ("d:\\1\\n2.txt", new_text); | |
| text = readText("d:\\1\\n2.txt"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment