1 /* 2 * Copyright 2016 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.examples.nio.snippets; 18 19 import java.io.IOException; 20 import java.net.URI; 21 import java.nio.charset.StandardCharsets; 22 import java.nio.file.FileSystem; 23 import java.nio.file.FileSystems; 24 import java.nio.file.Files; 25 import java.nio.file.Path; 26 import java.util.List; 27 28 /** 29 * A snippet showing how to get a {@link FileSystem} instance for a Google Cloud Storage bucket. 30 * This snippet also shows how to create a file and read its lines. 31 */ 32 public class GetFileSystem { 33 main(String... args)34 public static void main(String... args) throws IOException { 35 FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket")); 36 byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); 37 Path path = fs.getPath("/object"); 38 Files.write(path, data); 39 List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); 40 } 41 } 42