Wel, I have discovered, how to copy one text file to another,yet. (See code bellow). However, I really need to Cap every word in the source text file and copy the text that way to the second file. But I'm desperate. I tried few ways to make it (via Character.toUpperCase()) and there was still an error. Could you help me, please?

Thanks in advance.

import java.io.*;
import java.util.*;

public class CopyTextFile {

    public static void main(String args[]) {
        //... Get two file names from use.
        System.out.println("Enter a filepath to copy from, and one to copy to.");
        Scanner in = new Scanner(System.in);

        //... Create File objects.
        File inFile  = new File(in.next());  // File to read from.
        File outFile = new File(in.next());  // File to write to

        //... Enclose in try..catch because of possible io exceptions.
        try {
            copyFile2(inFile, outFile);

        } catch (IOException e) {
            System.err.println(e);
            System.exit(1);
        }
    }


    //=============================================================== copyFile
    // Uses BufferedReader for file input.
    public static void copyFile(File fromFile, File toFile) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(fromFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));

        //... Loop as long as there are input lines.
        String line = null;
        while ((line=reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();   // Write system dependent end of line.
        }

        //... Close reader and writer.
        reader.close();  // Close to unlock.
        writer.close();  // Close to unlock and flush to disk.
    }


    //=============================================================== copyFile2
    // Uses Scanner for file input.
    public static void copyFile2(File fromFile, File toFile) throws IOException {
        Scanner freader = new Scanner(fromFile);
        BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));

        //... Loop as long as there are input lines.
        String line = null;
        while (freader.hasNextLine()) {
            line = freader.nextLine();
            System.out.print(line+"*");

            writer.write(line);
            writer.newLine();   // Write system dependent end of line.
        }

        //... Close reader and writer.
        freader.close();  // Close to unlock.
        writer.close();  // Close to unlock and flush to disk.
    }

}

Edited 3 Years Ago by pritaeas: Fixed formatting

Does the code that you provided works? Because it seems OK.
What errors do you get?
Also, check the API for the String class. There should be a method for turning a String to upppercase

> writer.write(line); writer.write(line.toUpperCase()); Since you are reading in a line from the file in a String, consider looking at the java.lang.String class documentation.

> writer.write(line); writer.write(line.toUpperCase()); Since you are reading in a line from the file in a String, consider looking at the java.lang.String class documentation.

Well, but this command cap all letters. And I'm interested in the first ones only.

Example: The source text file contains: I would appreciate your help. I don't know how to copy it to a text file, which should contain: I Would Appreciate Your Help.

See? I don't know, how to cap the first letter of every single word in the copied file. Any idea?

Ah, I misread the question.

Anyways, use the String companion mutator class i.e. StringBuilder to process the line read since Strings in Java are immutable.

StringBuilder buf = new StringBuilder(line);
// Process this buffer character by character, ignore whitespaces
// and capitalize the first non-whitespace character.

Look into the Character wrapper class for utility methods to convert a given character to uppercase.

I see there could be a way, however I dont see how to make it. :( Could you show an example? When I have a text file, how to upcase it (1st letters? I know the solution is somewhere near, but I can't reach it. :(

This article has been dead for over six months. Start a new discussion instead.