Skip to content

Instantly share code, notes, and snippets.

@gmakc-094423
Created September 14, 2022 21:25
Show Gist options
  • Save gmakc-094423/15ec35260fff3642c9f34b4e73703714 to your computer and use it in GitHub Desktop.
Save gmakc-094423/15ec35260fff3642c9f34b4e73703714 to your computer and use it in GitHub Desktop.
Домашнее задание к 2 семинару
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class dz_02_1 {
public static void main(String[] args) throws IOException {
// Path dir = Path.of("C:\\Program Files\\Java\\jdk-11.0.16\\conf\\management",
// "//");
Path dir = Paths.get("C:\\Program Files\\Java\\jdk-18.0.2.1\\bin\\server");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
String str = entry.toString();
String substr = str.substring(str.lastIndexOf('.') + 1);
System.out.printf("Расширение файла: %s\n", substr);
}
}
}
}
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class dz_02_2 {
public static void main(String[] args) {
String begin = new String("select * from students where ");
JSONParser parser = new JSONParser();
try (Reader reader = new FileReader("filterstring.json")) {
JSONObject jsonObject = (JSONObject) parser.parse(reader);
System.out.printf("Filter: %s\n", jsonObject);
String sb = createString(begin, jsonObject);
System.out.printf("Rezult: %s\n", sb);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
static String createString(String begin, JSONObject jsonObject) {
StringBuilder sb = new StringBuilder(begin);
for (Object key : jsonObject.keySet()) {
String keyStr = (String) key;
String keyvalue = (String) jsonObject.get(keyStr);
if (!keyvalue.equals("Null")) {
sb.append(keyStr + " = '" + keyvalue + "' and ");
}
}
sb.delete(sb.lastIndexOf("and"), sb.length());
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment