C# 用StreamWriter进行文件输入的时候如何格式化输出

来源:百度知道 编辑:UC知道 时间:2024/05/10 11:35:57
比如说我的文本里面输入一个二维数组的值,我想让它们占有相同的位数并且右对齐,应该怎么做?
感谢yenange这么快给出办法。这个\t我以为可以用Tab替代,最后发现用\t的效果美观多了。谢谢。
但还想追问下,\t是左对齐,有没有办法右对齐呢?

还继续问下,怎么在文件的最前面输入一串字符?
true控制的是在文件最后追加,不合要求。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication4 //改成你的命名空间
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double[,] a = { { 0, 1, 2, 3 }, { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };
private void button1_Click(object sender, EventArgs e)
{
//让用户选择保存文件的位置及名字
SaveFileDialog sfgTxt = new SaveFileDialog();
sfgTxt.Filter = "txt文件|*.txt";
DialogResult dr = sfgTxt.ShowDialog();
if (dr != DialogResult.OK) { return; }

//创建文件流
FileStream fs = new FileStream(sfgTxt.FileName,FileMode.Create);