Created
November 9, 2017 09:28
-
-
Save KatagiriSo/f2be588ff72ba5b0619e608cc4db808c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RDFileUtil.swift | |
// RDFileUtil | |
// | |
// Created by KatagiriSo on 2017/11/09. | |
// Copyright © 2017年 rodhosSoft. All rights reserved. | |
// | |
import Foundation | |
struct RDFileUtil { | |
static func showCurentList() { | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
let contents = try! fm.contentsOfDirectory(atPath: path) | |
print(contents.description) | |
} | |
static func getCurrentList() -> [String] { | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
let contents = try! fm.contentsOfDirectory(atPath: path) | |
return contents | |
} | |
static func getCurrentFilePathList() -> [String] { | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
let contents = try! fm.contentsOfDirectory(atPath: path) | |
let contentPaths = contents.map { path + "/" + $0} | |
let files = contentPaths.filter { | |
var isDir : ObjCBool = false | |
fm.fileExists(atPath: $0, isDirectory: &isDir) | |
if isDir.boolValue { | |
return false | |
} else { | |
return true | |
} | |
} | |
return files | |
} | |
static func showCurrentPath() { | |
let path = FileManager().currentDirectoryPath | |
print(path) | |
} | |
static func createFile(name:String) { | |
let fm = FileManager() | |
let path = FileManager().currentDirectoryPath | |
fm.createFile(atPath: path + "/" + name, contents: nil, attributes: nil) | |
} | |
static func writeFile(fileName:String, content:String) { | |
print(#function + " " + fileName) | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
try! content.write(toFile: path + "/" + fileName, atomically: true, encoding: .utf8) | |
} | |
static func getFileNames() -> [String] { | |
return getCurrentFilePathList().map { | |
URL(string:$0)!.lastPathComponent.components(separatedBy: ".").first!}.filter { !$0.isEmpty } | |
} | |
static func changeSwift() -> [String] { | |
return getFileNames().map { $0 + ".swift"} | |
} | |
static func readFile(fileName:String) -> String { | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
let text = try! String( contentsOfFile: path + "/" + fileName, encoding: .utf8 ) | |
return text | |
} | |
static func appendText(fileName: String, string: String) { | |
let fm = FileManager() | |
let path = fm.currentDirectoryPath | |
let url = URL(fileURLWithPath: path + "/" + fileName) | |
do { | |
let fileHandle = try FileHandle(forWritingTo: url) | |
let text = "\n" + string | |
fileHandle.seekToEndOfFile() | |
fileHandle.write(text.data(using: String.Encoding.utf8)!) | |
} catch let error as NSError { | |
print("appendText failed: \(error)") | |
} | |
} | |
static func createSwiftFiles() { | |
changeSwift().forEach { | |
print("createFile " + $0) | |
createFile(name: $0) | |
writeFile(fileName: $0, content: "hello") | |
appendText(fileName: $0, string: "add") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment