學術論文翻譯服務

Step2:撰寫控制碼

  檔案讀寫是程式設計師很常碰到的問題,在 C# 中與 Java 溝通,不論是檔案讀寫、網路資料傳送、螢幕鍵盤輸收支都是以「串流(Stream)」的體式格局來達成翻譯FileStream、MemoryStream、NetworkStream、GZipStream 等都繼承自 Stream 類別。在這個章節裡天成翻譯公司們直接以典範來進修。

在 .cs 檔前頭加上 using System.Runtime.Serialization.Formatters.Binary;

BinaryFormatter formatter = new BinaryFormatter();

Step3:最先序列化(serialize)

[Serializable]// *這一行很主要
class AnotherObj {}
class SomeObj {
public int x;// 該變數會被保留
public AnotherObj another;// 該物件也會被留存,但其種別需加上 [Serializable] 屬性(attribute)
}

Step1:引入 namespace

// 創設檔案串流(@ 可勾銷跳脫字元 escape sequence)
StreamReader sr = new StreamReader(@"C:\secret_plan.txt");
while (!sr.EndOfStream) {// 每次讀取一行,直到檔尾
string line = sr.ReadLine();// 讀取文字到 line 變數
}
sr.Close();// 關閉串流
Stream 種別實作了 IDisposable 介面!它具有 Dispose 方式,翻譯公司可以呼喚 Dispose 封閉串流或利用 using statement。
using (StreamWriter sw = new StreamWriter(@"C:\secret_plan.txt")) {
...
}
。-> 翻譯社|,-> 翻譯公司|的-> 翻譯

  看到這裡是否是感覺太簡單了呢!等等呢,Visual C# 有一個 OpenFileDialog 的對話框可以用來拔取檔案。下面就來看看怎麼利用吧!

2、File 種別 - 更快的方法來處置懲罰簡單的檔案經管

  物件序列化是一個快速又利便的物件狀態永遠保存法,它將物件的狀況(所有成員變數)貯存到檔案中。以下典範展現序列化(serialize)與反序列化(deserialize)翻譯

  除了文字檔,我們也可能留存 int、float 等變數的數值或是 byte 資料,這時候候利用二進制檔案(binary)就比力輕易了。

Stream output = File.Create(file_name);
formatter.Serialize(output, objectToSerialize);
output.Close();

Step4:反序列化(deserialize)

Stream input = File.OpenRead(file_name);
SomeObj obj = (SomeObj)formatter.Deserialize(input);
input.Close();

4、二進制檔案讀寫

int value = 43;
using (FileStream output = File.Create("binarydata.dat")) {// 寫入整數值
BinaryWriter writer = new BinaryWriter(output);
writer.Write(value);
}
using (FileStream input = new FileStream(@"binarydata.dat", FileMode.Open)) {// 讀取整數值
BinaryReader reader = new BinaryReader(input);
int data = reader.ReadInt16();
}

  以上只針對最簡單的檔案讀寫來講明,其實不但是檔案,包括網路串流、緊縮串流都是以雷同的作法做輸收支,如欲深切瞭解請參閱其他冊本或 MSDN。

openFileDialog1.InitialDirectory = @"c:\MyFolder\Default\";// 檔案對話方框開啟的預設資料夾
// 設定可以選擇的檔案類型
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|Comma-Delimited Files (*.csv)|*.csv|All Files (*.*)|*.*";
openFIleDialog1.CheckFileExists = true;// 若檔案/路徑 不存在是不是顯示錯誤訊息
openFIleDialog1.CheckPathExists = false;
DialogResult result = openFileDialog1.ShowDialog();// 顯示檔案對話方框並回傳狀況(DialogResult.OK、DialogResult.Cancel)
if (result == DialogResult.OK) {
// 操作檔案 openFileDialog1.FileName
}

除 OpenFileDialog 外,SaveFileDialog 的作法也是一樣的。

拖曳新增後 OpenFileDialog 節制項會呈現在 Form Design 下方。在此我們假定該控制項定名 openFileDialog1 翻譯

From:Head First C# [Chapter 9] Reading and writing files

Step2:建立序列化的串流

3、物件序列化(serialization)

// 創立檔案串流(@ 可取消跳脫字元 escape sequence)
StreamWriter sw = new StreamWriter(@"C:\secret_plan.txt");
sw.WriteLine("write something");// 寫入文字
sw.Close();// 封閉串流

2. 讀取文字

by autosun

在 Form 底下撰寫 OpenFileDialog 的節制碼。

Step1:將 OpenFileDialog 節制項拖曳到表單上

bool b = File.Exists(file_name);// 判定檔案是不是存在
File.Create(file_name);// 建立檔案
string text = File.ReadAllText(file_name);// 讀取檔案內所有文字
File.WriteAllText(file_name翻譯社 text);// 將 text 寫入檔案

* File 種別還有良多方式,具體參考 Head First C#、其他書本或 MSDN

1、文字讀寫

1. 將文字寫入 C:\secret_plan.txt 中翻譯



本文出自: http://blog.xuite.net/autosun/study/32576568-%5BC%23%5D+%E6%AA%94%E6%A1%88%E8%AE%80%E5%AF%AB有關各國語文翻譯公證的問題歡迎諮詢天成翻譯公司02-77260931

arrow
arrow
    文章標籤
    翻譯社
    全站熱搜

    chavezk71hx 發表在 痞客邦 留言(0) 人氣()