hello
everybody

I want to overwrite the names which are already existing and to append the new names in a file. can anybody plz help out.
if i use the FileWriter(Fname,boolean true),
it appends the even the ones which are existing, i want to append the new ones only.

regards
vps

Dani AI

Generated

Short summary and two safe patterns to solve this cleanly. The original problem from is usually one of two goals: (A) append only names that are not already present (avoid duplicates), or (B) update/replace existing entries with new data. was right that rewriting is the safest general approach; is right that RandomAccessFile can work if you control record layout. Below are practical, minimal examples and tips.

Read-then-append only new names (fast to implement, good for plain lists)
This reads existing lines into a Set (normalized for comparison), then appends only names that are not already present.

Path path = Paths.get("names.txt");
Set<String> existing = new HashSet<>();
if (Files.exists(path)) {
    try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
        lines.map(String::trim)
             .filter(l -> !l.isEmpty())
             .map(String::toLowerCase)    // optional: case-insensitive check
             .forEach(existing::add);
    }
}

// newNames is a List<String> of incoming names
List<String> toAppend = new ArrayList<>();
for (String nm : newNames) {
    String key = nm.trim().toLowerCase();
    if (!existing.contains(key)) {
        existing.add(key);
        toAppend.add(nm);
    }
}

if (!toAppend.isEmpty()) {
    Files.write(path, toAppend, StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
}

Rewrite/merge when you must update existing records
If the requirement is to replace old entries with newer ones, read into a Map keyed by the name or ID, merge new data, write out to a temp file and atomically replace the original (safer than in-place edits).

RandomAccessFile note
Use RandomAccessFile only when records have fixed byte length or you can reliably compute byte offsets. Otherwise in-place writes will corrupt the layout. For large datasets, consider an embedded DB (SQLite, H2) or an indexed file approach instead.

Extra tips
Trim and normalize (case, extra spaces) before comparing. Use an atomic move (or fall back if not supported) when rewriting. Consider file locks if multiple processes write concurrently.

Recommended Answers

All 2 Replies

You'll have to read the whole file into memory, make your changes, then rewrite it back out to the file, deleting the old one and recreating the new one. To be safe, recreate the file with a temporary name, then delete the old file, then rename the temp file.

Or better, use RandomAccessFile and just write the data you want at the place you want it selectively.
Takes some getting used to as you're no longer dealing with Writers but Streams but well worth is.
Typical sequence:
- move the file pointer
- create a buffer of data to write to the file
- write the buffer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.