Java 戈迪:真正的问题是:他为什么不创建一个数据库?如果您的项目太大,以至于您有3e10个用户id,那么将它们存储在文本文件中对我来说是不好的。或者只是解析文件并将用户id存储在int数组中。@Joey doh!是的,现在看来很明显 149905320 1165

Java 戈迪:真正的问题是:他为什么不创建一个数据库?如果您的项目太大,以至于您有3e10个用户id,那么将它们存储在文本文件中对我来说是不好的。或者只是解析文件并将用户id存储在int数组中。@Joey doh!是的,现在看来很明显 149905320 1165,java,file,random,Java,File,Random,戈迪:真正的问题是:他为什么不创建一个数据库?如果您的项目太大,以至于您有3e10个用户id,那么将它们存储在文本文件中对我来说是不好的。或者只是解析文件并将用户id存储在int数组中。@Joey doh!是的,现在看来很明显 149905320 1165665384 66969324 886633368 1145241312 286585320 1008665352 1135545396 186217320 132577356 final String id = generateRandom


戈迪:真正的问题是:他为什么不创建一个数据库?如果您的项目太大,以至于您有3e10个用户id,那么将它们存储在文本文件中对我来说是不好的。或者只是解析文件并将用户id存储在int数组中。@Joey doh!是的,现在看来很明显
149905320
1165665384
66969324
886633368
1145241312
286585320
1008665352
1135545396
186217320
132577356
final String id = generateRandomUserId(random);

/**
 * Select random elements from the a big text file
 * 
 * @param userIdsSet2
 * @param r
 * @return
 */
private String generateRandomUserId(Random r) {

     File bigFile = new File("C:\\bigfile.txt");

     //randomly select elements from a big text file         


}
File f = new File("D:/abc.txt");
RandomAccessFile file;
try {
    file = new RandomAccessFile(f, "r");
    long file_size = file.length();
    long chosen_byte = (long)(Math.random() * file_size);

    file.seek(chosen_byte);

    for (;;)
    {
        byte a_byte = file.readByte();
        char wordChar = (char)a_byte;
        if (chosen_byte >= file_size || wordChar == '\n' || wordChar == '\r' || wordChar == -1) break;
        else chosen_byte += 1;
        System.out.println("\"" + Character.toString(wordChar)  + "\"");
    }

    int chosen = -1;
    if (chosen_byte < file_size) 
    {
        String s = file.readLine();
        chosen = Integer.parseInt(s);
        System.out.println("Chosen id : \"" + s  + "\"");
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;


public class Main {

    /**
     * WARNING : This piece of code requires that the input file terminates by a BLANK line !
     * 
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        File f = new File("D:/abc.txt");
        RandomAccessFile file;

        try {

            file = new RandomAccessFile(f, "r");
            long file_size = file.length();

            // Let's start
            long chosen_byte = (long)(Math.random() * (file_size - 1));
            long cur_byte = chosen_byte;

            // Goto starting position
            file.seek(cur_byte);

            String s_LR = "";
            char a_char;

            // Get left hand chars
            for (;;)
            {
                a_char = (char)file.readByte();
                if (cur_byte < 0 || a_char == '\n' || a_char == '\r' || a_char == -1) break;
                else 
                {
                    s_LR = a_char + s_LR;
                    --cur_byte;
                    if (cur_byte >= 0) file.seek(cur_byte);
                    else break;
                }
            }

            // Get right hand chars
            cur_byte = chosen_byte + 1;
            file.seek(cur_byte);
            for (;;)
            {
                a_char = (char)file.readByte();
                if (cur_byte >= file_size || a_char == '\n' || a_char == '\r' || a_char == -1) break;
                else 
                {
                    s_LR += a_char;
                    ++cur_byte;
                }
            }

            // Parse ID
            if (cur_byte < file_size) 
            {
                int chosen_id = Integer.parseInt(s_LR);
                System.out.println("Chosen id : " + chosen_id);
            }
            else
            {
                throw new Exception("Ran out of bounds. But this usually never happen...");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}