Re: creating/copying files..



Roedy Green wrote:

File by itself won't let you create an empty file. You can create with FileOutputStream and writing no bytes. Just close.


Are you sure about that? It seems to work just fine for me.

//code
import java.io.File;
import java.io.IOException;

public class Test {
	
	private void makeFile(){
		
		File file = new File(System.getProperty("user.home"),
							 "testfile.txt");
		
		System.out.println("Checking existence of file: "
				 			+ file.toString());
		if (file.exists()) {
			System.out.println("Found!");
		} else {
			System.out.println("File not found!" +
								"\nCreating file.");
			
			try {
				if (file.createNewFile()) {
					System.out.println("File created!");
				}
			} catch (IOException e) {
				System.out.println("Unable to create file!");
				e.printStackTrace();
			}
		}
		
	}
	
	
	public static void main(String[] args){
		Test t = new Test();
		t.makeFile();
		System.out.println("Done.");
		System.exit(0);
	}

}
// end code

Carl.
.



Relevant Pages

  • Re: Try...Catch across assemblies
    ... you will get a messagebox showing the error when run from ... The bad news is I don't entirely understand it. ... public class Test: Form ... private void LoadEventHandler ...
    (microsoft.public.dotnet.framework)
  • Unhandled Exceptions outside of VS
    ... Some of us on the .net framework were working on an issue that I've ... generate an unhandled exception. ... public class Test: Form ... private void LoadEventHandler ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Can’t copy to clipboard
    ... > at System.Windows.Forms.Clipboard.SetDataObject(Object data, Boolean copy) ... Dennis ... private void btnCopyAddress_Click ...
    (microsoft.public.dotnet.framework)
  • Re: Object or ArrayList
    ... hello william i have tried but when i try to retrive i dont get any properties. ... private void btnPlace_Click(object sender, System.EventArgs e) ...
    (microsoft.public.dotnet.languages.csharp)
  • Displaying a value stored in a variable
    ... The code I am writing is: ... private void button1_Click(Object sender, System.EventArgs e) ... int x = 6; ...
    (microsoft.public.dotnet.vjsharp)