Question

in JAVA, why my code can run, but it does not show any files on my...

in JAVA, why my code can run, but it does not show any files on my desktop.

Scanner k = new Scanner(System.in);
System.out.println(" Please enter the first filename: ");
String fileName1 = k.nextLine();
System.out.println(" Please enter the second filename: ");
String fileName2 = k.nextLine();
File f1 = new File(fileName1);
Scanner file1 = new Scanner(f1);
PrintWriter f2 = new PrintWriter(fileName2);
  
while(file1.hasNext()){
String str= fileName1.toUpperCase();
f2.println(str.toUpperCase());
}
file1.close();
f2.close();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I made some changes and succefully it is showing all the files on desktop.You can see the changes in following code

import java.util.*;

import java.io.*;

class FileDemo

{

public static void main(String args[])

{

try

{

Scanner k = new Scanner(System.in);

System.out.println(" Please enter the first filename: ");

String fileName1 = k.nextLine();

System.out.println(" Please enter the second filename: ");

String fileName2 = k.nextLine();

PrintWriter f1 = new PrintWriter(new File(fileName1));

PrintWriter f2=new PrintWriter(new File(fileName2));

Scanner file1=new Scanner(new File(fileName1));

String str;

while(file1.hasNext())

{

str= fileName1.toUpperCase();

f2.println(str.toUpperCase());

}

f1.close();

f2.close();

}

catch(Exception e1)

{

System.out.println(e1);

}

}

}

Add a comment
Know the answer?
Add Answer to:
in JAVA, why my code can run, but it does not show any files on my...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • i have to write a program that asks the names of 2 files. the first should...

    i have to write a program that asks the names of 2 files. the first should be open for reading second for writing. the program should read the the contents of the first file change all characters to uppercase and store in the second file. the second file will be a copy of the first file, except that all characters will be uppercase. use notepad to test the program. i got this so far {         String firstfile;         String...

  • JAVA Which of these statements does not match by using exception action in Java? 1. Exception...

    JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods.    2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code.    3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • In JAVA, In two classes: As a zookeeper, it is important to know the activities of...

    In JAVA, In two classes: As a zookeeper, it is important to know the activities of the animals in your care and to monitor their living habitats. Create a monitoring system that does all of the following: -Asks a user if they want to monitor an animal, monitor a habitat, or exit -Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file and asks the user to enter one of...

  • Can anyone help me? Thank you very much. Question 10 1 pts The constructor below is...

    Can anyone help me? Thank you very much. Question 10 1 pts The constructor below is very similar to the one you used for the last assignment. What is its time complexity in terms of N, where N is the number of lines in the Input Stream (the word file)? public Doublets (Inputstream in) try t lexicon. new TreeSet String O; Scanner s new Scanner (new BufferedReader new I (in))) while (s hasNext String str s next boolean added lexicon....

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT