Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- public class Writer {
- /**
- * Read text file, for each line remove leading spaces and replace them with a count
- * of the number of spaces. Requires a text file "Squeeze.txt".
- */
- public static void main(String args[]) {
- String theText = "";
- String line;
- int c;
- try {
- Scanner in = new Scanner(new File("Squeeze.txt"));
- while (in.hasNextLine()) {
- line = in.nextLine();
- c=0;
- while (line.startsWith(" ")) {
- line = line.substring(1, line.length());
- c++;
- } // while line
- if (c>0) {
- theText += c;
- } // if
- theText += line + "\r\n";
- } // while in
- }
- catch(Exception e) {
- System.out.println("Error: " + e.getMessage());
- } // try/catch
- try {
- BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Squeeze2.txt")));
- bw.write(theText);
- bw.close();
- }
- catch (Exception e) {
- System.out.println("Couldn't write to file!");
- } // try/catch
- } // main()
- } // class Writer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement