有没有办法从集成终端打开当前 vscode 文本编辑器窗口中的文件,还是我总是必须使用侧面的文件浏览器?这对于打开不需要成为我当前工作区一部分的偶尔文件非常有帮助。此外,如果这允许我以 sudo 身份编辑文件,那就太棒了。
答案 0 :(得分:1)
您可以通过键入 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private final static String HOST = "localhost";
private final static int PORT = 2300;
private final static Scanner SCANNER = new Scanner(System.in);
private static Socket socket;
private static PrintWriter out;
private static BufferedReader in;
private boolean isRunning;
public Client() {
isRunning = true;
}
public void makeRequests() {
while (isRunning) {
try {
socket = new Socket(HOST, PORT);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Input number: ");
int requestedNumber = SCANNER.nextInt();
out.println(requestedNumber);
String response = in.readLine();
System.out.println("Response: " + response);
if (response.equals("The number is correct!")) {
System.out.println("Closing the client.");
System.exit(1);
}
} catch (UnknownHostException e) {
System.out.println("Could not find host.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Could not get a response.");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
client.makeRequests();
}
}
打开您所在的目录或使用 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Server extends Thread {
private String serverName;
private ServerSocket ss;
private BufferedReader in;
private PrintWriter out;
private int randomNumber;
private volatile boolean isRunning;
public Server(String serverName, ServerSocket ss) {
this.serverName = serverName;
this.ss = ss;
System.out.println("Server started: " + serverName + "listening at port: " + ss.getLocalPort());
isRunning = true;
randomNumber = generateRandomNumber(10);
start(); // Uruchomienie wątku
}
public int generateRandomNumber(int range) {
return new Random().nextInt(range);
}
public void run() {
while (isRunning) {
try {
Socket connection = ss.accept();
System.out.println("Connection established on server: " + serverName);
serviceRequests(connection);
} catch (IOException e) {
System.out.println("Connection closed.");
e.printStackTrace();
}
}
try {
ss.close();
System.out.println("Connection closed.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void serviceRequests(Socket connection) throws IOException {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out = new PrintWriter(connection.getOutputStream(), true);
try {
int requestNumber = Integer.parseInt(in.readLine());
if (requestNumber > this.randomNumber) {
System.out.println("The client guessed: " + requestNumber + ". Too high");
makeResponse("too high.");
} else if (requestNumber < this.randomNumber) {
System.out.println("The client guessed: " + requestNumber + ". Too low.");
makeResponse("too low.");
} else {
System.out.println("The client guessed: " + requestNumber);
System.out.println("CONGRATULATIONS, CORRECT!");
makeResponse("correct!");
ss.close();
}
} catch (NumberFormatException e) {
System.out.println("Could not parse request to integer.");
}
}
private void makeResponse(String message) throws IOException {
out.println("The number is " + message);
}
public static void main(String[] args) {
try {
InetSocketAddress isa = new InetSocketAddress("localhost", 2300);
ServerSocket ss = new ServerSocket();
ss.bind(isa);
new Server("test", ss);
} catch (IOException e) {
System.out.println("Could not create server socket.");
e.printStackTrace();
}
}
}
打开特定目录,您可以打开具有相同内容的文件 code .
您甚至可以打开多个文件,例如. code <dir name>
希望回答问题