gpt-4.1-mini-2025-04-14· OpenAI
LMArena Human Preference 140K
i need a java program that reads a text file then makes sure the links in it are not…
- User
i need a java program that reads a text file then makes sure the links in it are not duplicates
- Assistant
Certainly! Below is a simple Java program that reads a text file containing links (one per line), removes duplicate links, and then prints the unique links. You can easily modify it to write the unique links back to a file if needed.
import java.io.*; import java.nio.file.*; import java.util.*; public class RemoveDuplicateLinks { public static void main(String[] args) { if (args.length < 1) { System.out.println("Usage: java RemoveDuplicateLinks <input-file>"); return; } String inputFile = args[0]; Set<String> uniqueLinks = new LinkedHashSet<>(); // preserves insertion order try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFile))) { String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.isEmpty()) { uniqueLinks.add(line); } } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); return; } System.out.println("Unique links:"); for (String link : uniqueLinks) { System.out.println(link); } } }How to use:
- Save this code in a file named
RemoveDuplicateLinks.java. - Compile it:
javac RemoveDuplicateLinks.java - Run it with your input file as an argument:
java RemoveDuplicateLinks links.txt
Explanation:
- The program reads each line from the input file.
- It trims whitespace and ignores empty lines.
- It stores links in a
LinkedHashSetto automatically remove duplicates while preserving the original order. - Finally, it prints the unique links.
If you want me to help you modify this to write the unique links back to a file, just ask!
- Save this code in a file named
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.