Anonymous user
Append a record to the end of a text file: Difference between revisions
Append a record to the end of a text file (view source)
Revision as of 17:20, 12 September 2017
, 6 years ago→{{header|Java}}: code cleanup, consistent indentation
Thundergnat (talk | contribs) m (remove bogus template markup) |
(→{{header|Java}}: code cleanup, consistent indentation) |
||
Line 1,380:
=={{header|Java}}==
<lang java>import static java.util.Objects.requireNonNull;▼
<lang java>▼
import static java.nio.file.StandardOpenOption.APPEND;▼
import static java.util.Arrays.asList;▼
import static java.util.Arrays.stream;▼
▲import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;▼
import java.io.IOException;
Line 1,393 ⟶ 1,386:
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;
public class RecordAppender {
static class Record {
private final String account;
private final String password;
private final int uid;
private final int gid;
private final List<String> gecos;
private final String directory;
private final String shell;
public Record(String account, String password, int uid, int gid, List<String> gecos, String directory, String shell) {
this.account = requireNonNull(account);
this.password = requireNonNull(password);
this.uid = uid;
this.gid = gid;
this.gecos = requireNonNull(gecos);
this.directory = requireNonNull(directory);
this.shell = requireNonNull(shell);
}▼
@Override▼
public String toString() {
return account + ':' + password + ':' + uid + ':' + gid + ':' +
}▼
public static Record parse(String text) {▼
String[] tokens = text.split(":");▼
return new Record(▼
tokens[0],▼
Integer.parseInt(tokens[3]),
Arrays.asList(tokens[4].split(",")),▼
tokens[6]);
}
public static void main(String[] args) throws IOException {▼
▲ @Override
"jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,[email protected]:/home/jsmith:/bin/bash",▼
▲ return account + ':' + password + ':' + uid + ':' + gid + ':' + gecos.stream().collect(joining(",")) + ':' + directory + ':' + shell;
"jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,[email protected]:/home/jdoe:/bin/bash",▼
▲ }
"xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,[email protected]:/home/xyz:/bin/bash"▼
);
▲ public static Record parse(String text) {
▲ String[] tokens = text.split(":");
▲ return new Record(
▲ tokens[0],
Path tmp
Files.write(tmp, (Iterable<String>) records.stream().limit(2).map(Record::toString)::iterator);▼
▲ parseInt(tokens[3]),
▲ asList(tokens[4].split(",")),
Files.write(tmp,
▲ tokens[5],
▲ tokens[6]);
try (Stream<String> lines = Files.lines(tmp)) {▼
▲ }
lines.map(Record::parse).forEach(System.out::println);▼
}
▲ public static void main(String[] args) throws IOException {
▲ "jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,[email protected]:/home/jsmith:/bin/bash",
▲ "jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,[email protected]:/home/jdoe:/bin/bash",
▲ "xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,[email protected]:/home/xyz:/bin/bash"
▲ };
▲ List<Record> records = stream(rawData).map(Record::parse).collect(toList());
▲ Files.write(tmp, (Iterable<String>)records.stream().limit(2).map(Record::toString)::iterator);
▲ Files.write(tmp, asList(records.get(2).toString()), APPEND);
▲ try(Stream<String> lines = Files.lines(tmp)) {
▲ lines.map(Record::parse).forEach(System.out::println);
}
{{out}}
<pre>Appended Record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
|