Jump to content

Beginner JAVA Help. What does this code do?

abhigyan001

I am a beginner JAVA programmer, a friend of mine sent me this code which I clearly don't know does what. Help me guys.

 

import java.util.*;
import java.io.*;
public class createFile
{
public static void main(String args[])throws IOException
{

for(int i =1 ; i>=1 ; i++)
{
String y = Integer.toString(i);
String x = "C:\\";

        String z = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpppppppppppppppppppppppppppppppppp";
        String yk = "Ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
        String sc = z+yk;
        String gh = x+y;
        String lp = gh+".txt";
        File file = new File(lp);
        FileWriter pll = new FileWriter(file.getAbsolutePath());
        BufferedWriter bn = new BufferedWriter(pll);
        bn.write(sc);
        bn.close();
        System.out.println("DONE");
    }
}
}

 

Tell me what executing this code will do.

Hardcore Hardware Whore | Check out my Blog for for Latest Tech News and more | Cricket Fan | Weapons Nut | 

Link to comment
Share on other sites

Link to post
Share on other sites

Follow it through.

 

Starts with a loop. It'll only run once. It creates an integer (i) with a value of one, then sets the loop condition to check if i is greater than or equal to one, and to increase the value after the loop runs (i++).

 

Then it just runs through a bunch of variables.

First, it sets variable y to the value of i as a string (type casting), so y = "1"

Then it sets variable x to "C:\\", the first backslash escapes the second so it actually assigns the value correctly. It'd probably error when running without it.

Next it creates two strings, z and yk, with some random text. It's not important what the values of these strings are.

It then creates a variable sc that contains the values of z + yk, so sc now becomes both of those two random strings of text in the previous two lines.

Next it creates variable gh and assigns it the value of x and y, so gh = "c:\\1"

Next line creates variable lp, assigns it gh+".txt", so lp = "c:\\1.txt"

Now it creates the actual file (c:\1.txt) on your hard disk.

Next line opens that file so that the following line can write data to it.

The line after that (bn.write(sc)) will write the data in the sc variable (the two random strings that were concatenated)

Then it closes the writer stream and tells you it's done. 

 

When it's done, you should be able to open c:\1.txt and see the data written to the file. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/23/2016 at 4:28 PM, Toxictaru said:

Follow it through.

 

Starts with a loop. It'll only run once. It creates an integer (i) with a value of one, then sets the loop condition to check if i is greater than or equal to one, and to increase the value after the loop runs (i++).

 

Then it just runs through a bunch of variables.

First, it sets variable y to the value of i as a string (type casting), so y = "1"

Then it sets variable x to "C:\\", the first backslash escapes the second so it actually assigns the value correctly. It'd probably error when running without it.

Next it creates two strings, z and yk, with some random text. It's not important what the values of these strings are.

It then creates a variable sc that contains the values of z + yk, so sc now becomes both of those two random strings of text in the previous two lines.

Next it creates variable gh and assigns it the value of x and y, so gh = "c:\\1"

Next line creates variable lp, assigns it gh+".txt", so lp = "c:\\1.txt"

Now it creates the actual file (c:\1.txt) on your hard disk.

Next line opens that file so that the following line can write data to it.

The line after that (bn.write(sc)) will write the data in the sc variable (the two random strings that were concatenated)

Then it closes the writer stream and tells you it's done. 

 

When it's done, you should be able to open c:\1.txt and see the data written to the file. 

Minor correction, it doesn't run only once, it's an infinite loop. The constraint is i >= 1, so it goes on and on until it overflows (well, not infinite, but you get the point).

It basically creates a lot of files on your drive to occupy space.

----Let's break it apart!!-----

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×