-6

I want to create a file in windows with java program with the help of the attached program but whenever I do that the extension name appears in the file name.I want extension of this file to be txt on windows.How do I rectify this problem.

import java.io.*;

public class Program {
    public static void main(String[] args) {
        String text = "Hello world";
        try {
            File file = new File("example.txt");
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(text);
            output.close();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }    
}
Krishna
  • 770
  • 2
  • 8
  • 21
  • 3
    What do you mean by "the extension name appears in the file name"? Where does it appear? If you were creating a file `example.txt` but the `.txt` is showing up in a Windows Explorer window and you weren't expecting it to, the problem is with your Windows settings, not with the Java program. – ajb Nov 15 '13 at 16:58
  • Sorry, this is windows filenames question, not Java. – MariuszS Nov 15 '13 at 17:06
  • 1
    Ehh, this is completly diffrent question now :/ – MariuszS Nov 15 '13 at 17:07
  • you mean the solution provided by @user2720864 is irrelevant. – Krishna Nov 15 '13 at 17:09
  • No I mean, your question is irrelevant :) Your problem is wrong filename or wrong filename visible in Windows? What is your problem? Is filename ok? – MariuszS Nov 15 '13 at 17:12
  • 4
    I think you need to delete this question and ask a new one, probably with a "windows-explorer" tag. If you create a file whose name ends in `.txt`, it will have a `.txt` extension. If you want a file with a `.txt` extension but you don't want to see the `.txt` in a list of files, then it's a Windows Explorer question. It has nothing to do with your program or with any programming language. – ajb Nov 15 '13 at 17:13
  • I think he will still be able to open it with notepad .. only he needs to choose the program to open with manually – user2720864 Nov 15 '13 at 17:17
  • Are all the other text files on your computer also shown as example2.txt? And this file shows as example.txt (but not example.txt.txt or anything stupid like that) – Richard Tingle Nov 15 '13 at 17:21
  • I think @ajb is right, this is the problem with windows-explorer – Krishna Nov 15 '13 at 17:30
  • @Krishna "Problem" is possibly a bit harsh. Personally I hate it when an operating system has been set up so the file extention isn't shown – Richard Tingle Nov 15 '13 at 22:14

3 Answers3

2

Just remove the .txt extension like

    File file = new File("example")
user2720864
  • 8,015
  • 5
  • 48
  • 60
1

Change

File file = new File("example.txt")

To

File file = new File("example")
Mengjun
  • 3,159
  • 1
  • 15
  • 21
0

This is simple, remove extension from filename example.txt -> example. Read about file extensions. The file format is not changed. To open this file simply use Load any file option.

public static void main(String[] args) {
    String text = "Hello world";
    try {
        File file = new File("example");
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

You can read about removing filename extension here: How do I trim a file extension from a String in Java?

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155