// show me C# code how to read from the console and print it out
result:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
}
}
}
Console.WriteLine("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
// show me Java code how to read from the console and print it out
import java.util.Scanner;
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello " + name);
scanner.close();
}
}
# show me python code how to read from the console and print it out
import sys
print(sys.stdin.read())
// show me C# code how to make a web request to google.com, fetch its logo
using System;
using System.Net;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
WebRequest request = WebRequest.Create("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string fileName = Path.GetFileName(request.RequestUri.AbsolutePath);
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
using (FileStream fileStream = File.Create(filePath))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = data.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
as usual, we see that we can shorten this code a bit using modern C#// show me C# code how to make a web request to google.com, fetch its logo
using System;
using System.Net;
using System.IO;
WebRequest request = WebRequest.Create("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string fileName = Path.GetFileName(request.RequestUri.AbsolutePath);
string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
using (FileStream fileStream = File.Create(filePath))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = data.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}
// show me java code how to make a web request to google.com, fetch its logo and store it as file logo.png
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class App {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("logo.png");
byte[] buffer = new byte[2048];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
import requests
r = requests.get('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png')
with open('logo.png', 'wb') as f:
f.write(r.content)
// show me how to zip a directory of files
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
// show me how to zip a directory of files
using System.IO;
using System.IO.Compression;
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
// show me how to zip a directory of files
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class App {
public static void main(String[] args) throws Exception {
String sourceFile = "C:\\Users\\jason\\Desktop\\test";
FileOutputStream fos = new FileOutputStream("C:\\Users\\jason\\Desktop\\test.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(sourceFile);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws Exception {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
}
# zip all the files in a directory into backup.zip
import zipfile
import os
with zipfile.ZipFile('backup.zip', 'w') as myzip:
for file in os.listdir():
myzip.write(file)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.