• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Creating file on a certain folder based on input

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

I have a series of classes that take 3 files as input, and create 3 files as output, the problem is that these 3 output files are not being created. I want them to be created on the same folder as the .java and .class files. These are the 2 things that I tried:



This piece of code works if I compile the .java files as a package, meaning that inside of my folder, I create another folder with the .class files in there (by doing "javac -d .")

But this is not what Im looking for, so I also tried:

instead of the previous File line. And this one still doesn't work. The output should be something like "file4.txt" and I want to create a file called file4.txt inside the folder P6b, and I dont get why none of these two options work.

EDIT: The full code:
 
Marshal
Posts: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always indent your code correctly and space it out; what you showed is very difficult to read. If I find it difficult to read, you will find it much harder.
How are you closing your print writer? You didn't show us that. If you fail to close it, there is a risk of the files not being written.

drcipres perez wrote:. . .. . .

Please always post exactly the same code as you are using; what you showed won't compile.
Stop using File; it is regarded as legacy code. More details in the Java™ Tutorials.

Why are you using so many command‑line arguments? Why have you got so much code in the main() method? Why are you keeping your work files and the code in the same directory? I don't like any of those last three things.
 
Saloon Keeper
Posts: 28660
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caution: people who code empty catch clauses risk physical assault. ALWAYS do something in your catch clause, even if it's only a printStackTrace()! Never silently eat exceptions or you'll end up not knowing what happened. And if you come for help from me, neither will I, which is where the risk of assault comes from.

Also, "if (xyz == true)" in Java is a sign of ignorance. Since xyz has to be boolean, the comparison is redundant. Just say "if (xyz) {..."
 
Campbell Ritchie
Marshal
Posts: 80636
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid editing your posts after they have been replied to; in this case it does no harm, so I shall let the changes stand. You have added the whole code to your original post.
Please beware of comments; a comment should always clarify things, but some, e.g....add nothing new. Please don't write ALL UPPER CASE. That comment does raise a question: why have you not written a constructor for that class? I don't like default constructors.
Some other comments, e.g. ...are incorrect. That doesn't write anything to the file. You appear to be opening the print writer and closing it before using it. Anyway, that isn't the correct way to close your writer. Look in the Java™ Tutorials.You can wrap that sort of code in a loop, but it really merits a method to itself, in which case I would allow the exception to propagate by deleting the catch and declaring that the method throws FileNotFoundException.It might be worth finding out how to use a JFileChooser to choose the files.
I think you should move reading and writing the files into different methods. Your file reading can be called from a constructor, but methods called from a constructor should have private access or be marked final. (It is not necessary for a method to be both final and private.) Again don't call close() on a Scanner, but use try with resources instead.
I don't like nextLine() on Scanners; it is better if at all possible to divide the line into parts and read the individual parts with next(), nextInt(), nextBigDecimal(), etc.
Don't write == true or == false. Both are poor style and error‑prone because you might write = by mistake.
Never while (b == true) ... please.
Always while (b) ... please.
Never while (b == false) ... please.
Always while (!b) ... please.

[edit] Sorry I was mistaken; you do appear to be writing something in that file. I have struck out the line where I was mistaken above. The line l5 I mentioned doesn't write anything to the file. It might create the file, but any writing occurs later.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic