• Post Reply Bookmark Topic Watch Topic
  • New Topic

cant create a new file  RSS feed

 
simrat khanuja
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:

import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try
{
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter the name of the new file");
String filename=din.readLine();
File f=new File(filename);
FileOutputStream fout=new FileOutputStream(f);
DataOutputStream dout=new DataOutputStream(fout);
System.out.println("enter your name ");
String n=din.readLine();
dout.writeChars("hello "+ n);
System.out.println("check your file..");
}
catch(Exception e)
{}
}
}

error :

d:\jpro>javac FileWrite.java
FileWrite.java:11: error: constructor File in class File cannot be applied to given types;
File f=new File(filename);
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
FileWrite.java:12: error: no suitable constructor found for FileOutputStream(File)
FileOutputStream fout=new FileOutputStream(f);
^
constructor FileOutputStream.FileOutputStream(FileDescriptor) is not applicable
(actual argument File cannot be converted to FileDescriptor by method invocation conversion)
constructor FileOutputStream.FileOutputStream(java.io.File,boolean) is not applicable
(actual and formal argument lists differ in length)
constructor FileOutputStream.FileOutputStream(java.io.File) is not applicable
(actual argument File cannot be converted to java.io.File by method invocation conversion)
constructor FileOutputStream.FileOutputStream(String,boolean) is not applicable
(actual and formal argument lists differ in length)
constructor FileOutputStream.FileOutputStream(String) is not applicable
(actual argument File cannot be converted to String by method invocation conversion)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simrat khanuja wrote:



There is in fact such a constructor in the File class that is part of core Java -- so this error doesn't make sense.

Do you happen to have your own File class?

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some potentially dangerous errors in that code. You are creating writers and not closing them (try with resources would be the best way to close them) and you are ignoring a potential Exception.
You mightget no output if you don't close the writers.
 
simrat khanuja
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, its not compiling..
this same program has compiled in other IDEs but is failing to compile in cmd.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simrat khanuja wrote:no, its not compiling..
this same program has compiled in other IDEs but is failing to compile in cmd.


Did you check to see if you happen to have your own File class?

Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something else must be going on - it compiles fine for me on the command line. I agree with Henry - it seems like you have your class called "File" somewhere; is that true? If so, rename it to something that doesn't clash with the java.io.File class.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simrat khanuja wrote:
constructor FileOutputStream.FileOutputStream(java.io.File) is not applicable
(actual argument File cannot be converted to java.io.File by method invocation conversion)


More proof. This error is saying that the File class that it found is not the java.io.File class that it was expecting. So, you definitely has a different File class somewhere.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!