-4

This program creates 10 files on my desktop. The issue I am having is with the file names. The first one created is called "SecretFile1". The second is "SecretFile12". The third is "SecretFile123". What changes should I make so that the file names are "SecretFile1", "SecretFile2", and "SecretFile3" respectively?

import java.io.*;

public class TextFiles {

    public static void main(String[] args) throws IOException {
        String doc = "SecretFile";
        int number = 0;
        for(i = 1; i <= 10;i++){

            number++;

            doc = doc + number;
            String name = "C:\\Users\\Soumil\\Desktop\\" + doc + ".txt";

            BufferedWriter bw = new BufferedWriter(new FileWriter("" + name + ""));
            bw.write("There's no secret.");
            bw.close();
       }
   }
}
5
  • 2
    It's import java.util.Scanner;. util not io. Voting to close as a trivial typographical error. Commented Feb 27, 2016 at 16:52
  • How can you even make that typo? Did you really type the import by hand? Commented Feb 27, 2016 at 16:52
  • @Tunaki, I guess they are just using a text editor rather than an IDE? Commented Feb 27, 2016 at 16:53
  • Try to use this import for Scanner class not import java.io.Scanner;: java.util.Scanner Commented Feb 27, 2016 at 16:53
  • @user3370908 That's a different error. Please, take your time to think this through, read your code again, read the documentation. Commented Feb 27, 2016 at 16:56

2 Answers 2

1

Use this code, it will work!

String doc = "SecretFile";
        String dump="";
           int number = 0;
           for(int i = 1; i <= 10;i++){

               number++;

               dump = doc + number;
               String name = "F:/src/" + dump + ".txt";

               BufferedWriter bw = new BufferedWriter(new FileWriter("" + name + ""));
               bw.write("There's no secret.");
               bw.close();
          }
Sign up to request clarification or add additional context in comments.

Comments

-1

For reading data from file, you can use this code

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class TextFiles {

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("F:/input.txt");
        InputStreamReader input = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(input);
        String data;
        String result = new String();

        try {
            while ((data = br.readLine()) != null) {
                result = result.concat(data + " ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(result);
    }
}

5 Comments

Very poor code-only answer without any explanation.
I am an optimistic man. I have just tried to give him some quell, nothing else. Senior Bosses are already make this question as silly. :) When I was writing the answer, I know all people will give me curse...@Tunaki
I have some words in my text.txt , but when I try to print n or h nothing comes out.
You would help him better by explaining what the code does, how it does it and what he needs to change and why.
Follow this - stackoverflow.com/questions/2864117/… @user3370908 It will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.