Solved

generate random number of string with java

Posted on 2009-05-12
1
1,494 Views
Last Modified: 2013-11-23
i want to generate around 10000 lines of random strings and input it to file in java. Please tell me how to do it. Also tell me how to reach to the end of this file after generating the lines. Also tell me how to find the length of the file and the number of lines in this file. Please do not use any system commands or tail command
0
Comment
Question by:SunScreenCert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
sundar62 earned 125 total points
ID: 24371689
Please find the code snippet below to generate the random number. you can get the number of lines of the file from the attached link -

http://www.faqs.org/docs/javap/c10/ex-10-2-answer.html

You may modify the code accordingly..
import java.security.SecureRandom;
import java.io.*;
 
public class GUID {
 
   private static SecureRandom s_secureRandom = new SecureRandom();
 
   public static void main(String[] args) throws Exception {
 
	   File file = new File("write.txt");
	   PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    
	   for (int i = 0; i < 10000 ;i++ )
		   output.println(generate());
 
	   output.close();
	   System.out.println("Your file has been written");
      
    }
  
   /**
    * generate guid from time,addr,hash and rand
    */
   public static String generate() {
      StringBuilder stringBuilder = new StringBuilder();
      int time = (int) System.currentTimeMillis();
      int rand = s_secureRandom.nextInt();
      stringBuilder.append(hexFormat(rand));
      return stringBuilder.toString();
   }
 
   /**
    * get int as 8-char string
    */
 
   private static String hexFormat(int data) {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append("00000000");
      stringBuilder.append(Integer.toHexString(data));
      return stringBuilder.substring(stringBuilder.length() - 8);
   }
 
 
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question